Expand var-args before passing to KeyGenerator

Update `CacheAspectSupport` to expand any var-arg parameters before
calling `KeyGenerator` implementations. Prior to this commit var-args
would be passed to `KeyGenerator` implementations as a nested array,
often causing the same key to be generated regardless of the arguments.

Issue: SPR-10870
This commit is contained in:
Phillip Webb
2013-09-23 15:14:58 -07:00
parent 2337e763d0
commit 05072e1762
10 changed files with 72 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -211,6 +211,19 @@ public abstract class AbstractAnnotationTests {
assertNotSame(r3, r4);
}
public void testVarArgsKey(CacheableService<?> service) throws Exception {
Object r1 = service.varArgsKey(1, 2, 3);
Object r2 = service.varArgsKey(1, 2, 3);
assertSame(r1, r2);
Object r3 = service.varArgsKey(1, 2, 3);
Object r4 = service.varArgsKey(1, 2);
assertNotSame(r3, r4);
}
public void testNullValue(CacheableService<?> service) throws Exception {
Object key = new Object();
assertNull(service.nullValue(key));
@@ -478,6 +491,11 @@ public abstract class AbstractAnnotationTests {
testKeyExpression(cs);
}
@Test
public void testVarArgsKey() throws Exception {
testVarArgsKey(cs);
}
@Test
public void testClassCacheCacheable() throws Exception {
testCacheable(ccs);

View File

@@ -89,6 +89,12 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default")
public Object varArgsKey(Object... args) {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name")
public Object name(Object arg1) {

View File

@@ -44,6 +44,8 @@ public interface CacheableService<T> {
T key(Object arg1, Object arg2);
T varArgsKey(Object... args);
T name(Object arg1);
T nullValue(Object arg1);

View File

@@ -91,6 +91,12 @@ public class DefaultCacheableService implements CacheableService<Long> {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default")
public Long varArgsKey(Object... args) {
return counter.getAndIncrement();
}
@Override
@Cacheable(value = "default", key = "#root.methodName")
public Long name(Object arg1) {