From 7be1b5c3f5e04de56aabbdf73f7aca9c14a5052d Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 2 Jun 2011 10:01:14 +0000 Subject: [PATCH] revised cache abstraction + remove generic signature on key generator (as the type is not used anywhere) + add a small improvement to CacheAspect to nicely handle the cases where the aspect is pulled in but not configured --- .../springframework/cache/KeyGenerator.java | 4 ++-- .../cache/interceptor/CacheAspectSupport.java | 23 ++++++++++++++----- .../cache/support/DefaultKeyGenerator.java | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/cache/KeyGenerator.java b/org.springframework.context/src/main/java/org/springframework/cache/KeyGenerator.java index d089dbfa488..b520929bb99 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/KeyGenerator.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/KeyGenerator.java @@ -24,7 +24,7 @@ import java.lang.reflect.Method; * * @author Costin Leau */ -public interface KeyGenerator { +public interface KeyGenerator { - K extract(Object target, Method method, Object... params); + Object extract(Object target, Method method, Object... params); } diff --git a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index 093ae72e07f..82ac363ae0d 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -68,7 +68,9 @@ public abstract class CacheAspectSupport implements InitializingBean { private final ExpressionEvaluator evaluator = new ExpressionEvaluator(); - private KeyGenerator keyGenerator = new DefaultKeyGenerator(); + private KeyGenerator keyGenerator = new DefaultKeyGenerator(); + + private volatile boolean initialized = false; public void afterPropertiesSet() { if (this.cacheManager == null) { @@ -78,6 +80,8 @@ public abstract class CacheAspectSupport implements InitializingBean { throw new IllegalStateException("Either 'cacheDefinitionSource' or 'cacheDefinitionSources' is required: " + "If there are no cacheable methods, then don't use a cache aspect."); } + + initialized = true; } /** @@ -110,7 +114,7 @@ public abstract class CacheAspectSupport implements InitializingBean { return keyGenerator; } - public void setKeyGenerator(KeyGenerator keyGenerator) { + public void setKeyGenerator(KeyGenerator keyGenerator) { this.keyGenerator = keyGenerator; } @@ -146,19 +150,26 @@ public abstract class CacheAspectSupport implements InitializingBean { } protected Object execute(Callable invocation, Object target, Method method, Object[] args) throws Exception { + // check whether aspect is enabled + // to cope with cases where the AJ is pulled in automatically + + if (!initialized) { + return invocation.call(); + } + + boolean log = logger.isTraceEnabled(); + // get backing class Class targetClass = AopProxyUtils.ultimateTargetClass(target); if (targetClass == null && target != null) { targetClass = target.getClass(); } - - boolean log = logger.isTraceEnabled(); - final CacheOperation cacheOp = getCacheDefinitionSource().getCacheOperation(method, targetClass); Object retVal = null; + // analyze caching information if (cacheOp != null) { CacheOperationContext context = getOperationContext(cacheOp, method, args, target, targetClass); @@ -262,7 +273,7 @@ public abstract class CacheAspectSupport implements InitializingBean { // context passed around to avoid multiple creations private final EvaluationContext evalContext; - private final KeyGenerator keyGenerator = CacheAspectSupport.this.keyGenerator; + private final KeyGenerator keyGenerator = CacheAspectSupport.this.keyGenerator; public CacheOperationContext(CacheOperation operation, Method method, Object[] args, Object target, Class targetClass) { diff --git a/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java b/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java index 3dff46c9f55..fd251f85c43 100644 --- a/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java +++ b/org.springframework.context/src/main/java/org/springframework/cache/support/DefaultKeyGenerator.java @@ -26,7 +26,7 @@ import org.springframework.cache.KeyGenerator; * * @author Costin Leau */ -public class DefaultKeyGenerator implements KeyGenerator { +public class DefaultKeyGenerator implements KeyGenerator { public Object extract(Object target, Method method, Object... params) { if (params.length == 1) {