Browse Source

SPR-8007

SPR-7832
+ expose the invocation params through the cache root object
+ update javadocs


git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4114 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/merge
Costin Leau 15 years ago
parent
commit
a653a50be8
  1. 10
      org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java
  2. 29
      org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheExpressionRootObject.java
  3. 33
      org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultCacheExpressionRootObject.java
  4. 6
      org.springframework.context/src/main/java/org/springframework/cache/interceptor/ExpressionEvaluator.java
  5. 21
      org.springframework.context/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java
  6. 5
      org.springframework.context/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java
  7. 2
      org.springframework.context/src/test/java/org/springframework/cache/config/CacheableService.java
  8. 5
      org.springframework.context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java
  9. 24
      spring-framework-reference/src/cache.xml

10
org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java vendored

@ -141,8 +141,8 @@ public abstract class CacheAspectSupport implements InitializingBean { @@ -141,8 +141,8 @@ public abstract class CacheAspectSupport implements InitializingBean {
}
protected CacheOperationContext getOperationContext(CacheDefinition definition, Method method, Object[] args,
Class<?> targetClass) {
return new CacheOperationContext(definition, method, args, targetClass);
Object target, Class<?> targetClass) {
return new CacheOperationContext(definition, method, args, target, targetClass);
}
@SuppressWarnings("unchecked")
@ -160,7 +160,7 @@ public abstract class CacheAspectSupport implements InitializingBean { @@ -160,7 +160,7 @@ public abstract class CacheAspectSupport implements InitializingBean {
// analyze caching information
if (cacheDef != null) {
CacheOperationContext context = getOperationContext(cacheDef, method, args, targetClass);
CacheOperationContext context = getOperationContext(cacheDef, method, args, target, targetClass);
Collection<Cache<?, ?>> caches = context.getCaches();
if (context.hasConditionPassed()) {
@ -277,13 +277,13 @@ public abstract class CacheAspectSupport implements InitializingBean { @@ -277,13 +277,13 @@ public abstract class CacheAspectSupport implements InitializingBean {
private final KeyGenerator<?> keyGenerator = CacheAspectSupport.this.keyGenerator;
public CacheOperationContext(CacheDefinition operationDefinition, Method method, Object[] args,
Class<?> targetClass) {
Object target, Class<?> targetClass) {
this.definition = operationDefinition;
this.caches = CacheAspectSupport.this.getCaches(definition);
this.method = method;
this.args = args;
this.evalContext = evaluator.createEvaluationContext(caches, method, args, targetClass);
this.evalContext = evaluator.createEvaluationContext(caches, method, args, target, targetClass);
}
/**

29
org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheExpressionRootObject.java vendored

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.cache.interceptor;
import java.lang.reflect.Method;
import java.util.Collection;
import org.springframework.cache.Cache;
@ -34,6 +35,34 @@ interface CacheExpressionRootObject { @@ -34,6 +35,34 @@ interface CacheExpressionRootObject {
*/
String getMethodName();
/**
* Returns the method being cached.
*
* @return method being cached
*/
Method getMethod();
/**
* Returns the parameters for this invocation.
*
* @return params for this invocation.
*/
Object[] getParams();
/**
* Returns the target instance being cached.
*
* @return target instance
*/
Object getTarget();
/**
* Returns the target class.
*
* @return target class
*/
Class<?> getTargetClass();
/**
* Returns the caches against which the method is executed.
*

33
org.springframework.context/src/main/java/org/springframework/cache/interceptor/DefaultCacheExpressionRootObject.java vendored

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.cache.interceptor;
import java.lang.reflect.Method;
import java.util.Collection;
import org.springframework.cache.Cache;
@ -28,12 +29,22 @@ import org.springframework.util.Assert; @@ -28,12 +29,22 @@ import org.springframework.util.Assert;
*/
public class DefaultCacheExpressionRootObject implements CacheExpressionRootObject {
private final Object target;
private final Class<?> targetClass;
private final String methodName;
private final Method method;
private final Collection<Cache<?, ?>> caches;
private final Object[] args;
public DefaultCacheExpressionRootObject(Collection<Cache<?,?>> caches, String methodName) {
Assert.hasText(methodName, "method name is required");
this.methodName = methodName;
public DefaultCacheExpressionRootObject(Collection<Cache<?, ?>> caches, Method method, Object[] args,
Object target, Class<?> targetClass) {
Assert.notNull(method, "method is required");
Assert.notNull(targetClass, "targetClass is required");
this.method = method;
this.methodName = method.getName();
this.target = target;
this.targetClass = targetClass;
this.args = args;
this.caches = caches;
}
@ -44,4 +55,20 @@ public class DefaultCacheExpressionRootObject implements CacheExpressionRootObje @@ -44,4 +55,20 @@ public class DefaultCacheExpressionRootObject implements CacheExpressionRootObje
public Collection<Cache<?, ?>> getCaches() {
return caches;
}
public Method getMethod() {
return method;
}
public Object[] getParams() {
return args;
}
public Object getTarget() {
return target;
}
public Class<?> getTargetClass() {
return targetClass;
}
}

6
org.springframework.context/src/main/java/org/springframework/cache/interceptor/ExpressionEvaluator.java vendored

@ -47,8 +47,10 @@ class ExpressionEvaluator { @@ -47,8 +47,10 @@ class ExpressionEvaluator {
private Map<Method, Expression> keyCache = new ConcurrentHashMap<Method, Expression>();
private Map<Method, Method> targetMethodCache = new ConcurrentHashMap<Method, Method>();
EvaluationContext createEvaluationContext(Collection<Cache<?, ?>> caches, Method method, Object[] args, Class<?> targetClass) {
DefaultCacheExpressionRootObject rootObject = new DefaultCacheExpressionRootObject(caches, method.getName());
EvaluationContext createEvaluationContext(Collection<Cache<?, ?>> caches, Method method, Object[] args,
Object target, Class<?> targetClass) {
DefaultCacheExpressionRootObject rootObject = new DefaultCacheExpressionRootObject(caches, method, args,
target, targetClass);
StandardEvaluationContext evaluationContext = new LazyParamAwareEvaluationContext(rootObject,
paramNameDiscoverer, method, args, targetClass, targetMethodCache);

21
org.springframework.context/src/test/java/org/springframework/cache/config/AbstractAnnotationTest.java vendored

@ -20,6 +20,7 @@ import static org.junit.Assert.*; @@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
@ -137,6 +138,16 @@ public abstract class AbstractAnnotationTest { @@ -137,6 +138,16 @@ public abstract class AbstractAnnotationTest {
assertTrue(cache.containsKey(keyName));
}
public void testRootVars(CacheableService service) {
Object key = new Object();
Object r1 = service.rootVars(key);
assertSame(r1, service.rootVars(key));
Cache<Object, Object> cache = cm.getCache("default");
// assert the method name is used
String expectedKey = "rootVarsrootVars" + AopProxyUtils.ultimateTargetClass(service) + service;
assertTrue(cache.containsKey(expectedKey));
}
@Test
public void testCacheable() throws Exception {
testCacheable(cs);
@ -205,4 +216,14 @@ public abstract class AbstractAnnotationTest { @@ -205,4 +216,14 @@ public abstract class AbstractAnnotationTest {
public void testClassMethodName() throws Exception {
testMethodName(ccs, "namedefault");
}
@Test
public void testRootVars() throws Exception {
testRootVars(cs);
}
@Test
public void testClassRootVars() throws Exception {
testRootVars(ccs);
}
}

5
org.springframework.context/src/test/java/org/springframework/cache/config/AnnotatedClassCacheableService.java vendored

@ -56,6 +56,11 @@ public class AnnotatedClassCacheableService implements CacheableService { @@ -56,6 +56,11 @@ public class AnnotatedClassCacheableService implements CacheableService {
return counter.getAndIncrement();
}
@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
public Object rootVars(Object arg1) {
return counter.getAndIncrement();
}
public Object nullValue(Object arg1) {
nullInvocations.incrementAndGet();
return null;

2
org.springframework.context/src/test/java/org/springframework/cache/config/CacheableService.java vendored

@ -40,4 +40,6 @@ public interface CacheableService<T> { @@ -40,4 +40,6 @@ public interface CacheableService<T> {
Number nullInvocations();
T rootVars(Object arg1);
}

5
org.springframework.context/src/test/java/org/springframework/cache/config/DefaultCacheableService.java vendored

@ -59,6 +59,11 @@ public class DefaultCacheableService implements CacheableService<Long> { @@ -59,6 +59,11 @@ public class DefaultCacheableService implements CacheableService<Long> {
return counter.getAndIncrement();
}
@Cacheable(value = "default", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
public Long rootVars(Object arg1) {
return counter.getAndIncrement();
}
@Cacheable("default")
public Long nullValue(Object arg1) {
nullInvocations.incrementAndGet();

24
spring-framework-reference/src/cache.xml

@ -176,6 +176,30 @@ public Book findBook(String name)]]></programlisting> @@ -176,6 +176,30 @@ public Book findBook(String name)]]></programlisting>
<entry>The name of the method being invoked</entry>
<entry><screen>#root.methodName</screen></entry>
</row>
<row>
<entry>method</entry>
<entry>root object</entry>
<entry>The method being invoked</entry>
<entry><screen>#root.method.name</screen></entry>
</row>
<row>
<entry>target</entry>
<entry>root object</entry>
<entry>The target object being invoked</entry>
<entry><screen>#root.target</screen></entry>
</row>
<row>
<entry>targetClass</entry>
<entry>root object</entry>
<entry>The class of the target being invoked</entry>
<entry><screen>#root.targetClass</screen></entry>
</row>
<row>
<entry>params</entry>
<entry>root object</entry>
<entry>The arguments (as array) used for invoking the target</entry>
<entry><screen>#root.params[0]</screen></entry>
</row>
<row>
<entry>caches</entry>
<entry>root object</entry>

Loading…
Cancel
Save