diff --git a/org.springframework.context.support/src/main/java/org/springframework/cache/interceptor/LazyParamAwareEvaluationContext.java b/org.springframework.context.support/src/main/java/org/springframework/cache/interceptor/LazyParamAwareEvaluationContext.java index 8cae8fb2e1e..abba7b9ccde 100644 --- a/org.springframework.context.support/src/main/java/org/springframework/cache/interceptor/LazyParamAwareEvaluationContext.java +++ b/org.springframework.context.support/src/main/java/org/springframework/cache/interceptor/LazyParamAwareEvaluationContext.java @@ -46,6 +46,8 @@ class LazyParamAwareEvaluationContext extends StandardEvaluationContext { LazyParamAwareEvaluationContext(Object rootObject, ParameterNameDiscoverer paramDiscoverer, Method method, Object[] args, Class targetClass, Map methodCache) { + super(rootObject); + this.paramDiscoverer = paramDiscoverer; this.method = method; this.args = args; diff --git a/org.springframework.context.support/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java b/org.springframework.context.support/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java index f0956745162..f654456dceb 100644 --- a/org.springframework.context.support/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java +++ b/org.springframework.context.support/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java @@ -20,6 +20,8 @@ import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -36,6 +38,8 @@ public abstract class AbstractAnnotationTest { protected CacheableService ccs; + protected CacheManager cm; + protected abstract String getConfig(); @Before @@ -43,6 +47,7 @@ public abstract class AbstractAnnotationTest { ctx = new ClassPathXmlApplicationContext(getConfig()); cs = ctx.getBean("service", CacheableService.class); ccs = ctx.getBean("classService", CacheableService.class); + cm = ctx.getBean(CacheManager.class); } public void testCacheable(CacheableService service) throws Exception { @@ -107,6 +112,16 @@ public abstract class AbstractAnnotationTest { assertEquals(nr + 1, service.nullInvocations().intValue()); } + public void testMethodName(CacheableService service, String keyName) + throws Exception { + Object key = new Object(); + Object r1 = service.name(key); + assertSame(r1, service.name(key)); + Cache cache = cm.getCache("default"); + // assert the method name is used + assertTrue(cache.containsKey(keyName)); + } + @Test public void testCacheable() throws Exception { testCacheable(cs); @@ -155,4 +170,14 @@ public abstract class AbstractAnnotationTest { assertEquals(nr + 1, AnnotatedClassCacheableService.nullInvocations .intValue()); } + + @Test + public void testMethodName() throws Exception { + testMethodName(cs, "name"); + } + + @Test + public void testClassMethodName() throws Exception { + testMethodName(ccs, "namedefault"); + } } \ No newline at end of file diff --git a/org.springframework.context.support/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java b/org.springframework.context.support/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java index 16510428d6c..2280ae21d55 100644 --- a/org.springframework.context.support/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java +++ b/org.springframework.context.support/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java @@ -47,6 +47,11 @@ public class AnnotatedClassCacheableService implements CacheableService { return counter.getAndIncrement(); } + @Cacheable(value = "default", key = "#root.methodName + #root.caches[0].name") + public Object name(Object arg1) { + return counter.getAndIncrement(); + } + public Object nullValue(Object arg1) { nullInvocations.incrementAndGet(); return null; diff --git a/org.springframework.context.support/src/test/java/org/springframework/cache/config/CacheableService.java b/org.springframework.context.support/src/test/java/org/springframework/cache/config/CacheableService.java index ebb8834da4e..5439ac7add7 100644 --- a/org.springframework.context.support/src/test/java/org/springframework/cache/config/CacheableService.java +++ b/org.springframework.context.support/src/test/java/org/springframework/cache/config/CacheableService.java @@ -32,6 +32,8 @@ public interface CacheableService { T key(Object arg1, Object arg2); + T name(Object arg1); + T nullValue(Object arg1); Number nullInvocations(); diff --git a/org.springframework.context.support/src/test/java/org/springframework/cache/config/DefaultCacheableService.java b/org.springframework.context.support/src/test/java/org/springframework/cache/config/DefaultCacheableService.java index 903e07d76e6..7dbcdb905c0 100644 --- a/org.springframework.context.support/src/test/java/org/springframework/cache/config/DefaultCacheableService.java +++ b/org.springframework.context.support/src/test/java/org/springframework/cache/config/DefaultCacheableService.java @@ -50,6 +50,11 @@ public class DefaultCacheableService implements CacheableService { return counter.getAndIncrement(); } + @Cacheable(value = "default", key = "#root.methodName") + public Long name(Object arg1) { + return counter.getAndIncrement(); + } + @Cacheable("default") public Long nullValue(Object arg1) { nullInvocations.incrementAndGet();