public abstract class CacheAspectSupport extends AbstractCacheInvoker implements InitializingBean, SmartInitializingSingleton, ApplicationContextAware
CacheInterceptor
or an AspectJ aspect.
This enables the underlying Spring caching infrastructure to be used easily to implement an aspect for any aspect system.
Subclasses are responsible for calling methods in this class in the correct order.
Uses the Strategy design pattern. A CacheResolver
implementation will resolve the actual cache(s) to use, and a
CacheOperationSource
is used for determining caching
operations.
A cache aspect is serializable if its CacheResolver
and
CacheOperationSource
are serializable.
Modifier and Type | Class and Description |
---|---|
private static class |
CacheAspectSupport.CacheOperationCacheKey |
protected class |
CacheAspectSupport.CacheOperationContext |
private class |
CacheAspectSupport.CacheOperationContexts |
protected static class |
CacheAspectSupport.CacheOperationMetadata
Metadata of a cache operation that does not depend on a particular invocation
which makes it a good candidate for caching.
|
private class |
CacheAspectSupport.CachePutRequest |
Modifier and Type | Field and Description |
---|---|
private ApplicationContext |
applicationContext |
private CacheOperationSource |
cacheOperationSource |
private CacheResolver |
cacheResolver |
private ExpressionEvaluator |
evaluator |
private boolean |
initialized |
private KeyGenerator |
keyGenerator |
protected Log |
logger |
private java.util.Map<CacheAspectSupport.CacheOperationCacheKey,CacheAspectSupport.CacheOperationMetadata> |
metadataCache
Cache of CacheOperationMetadata, keyed by
CacheAspectSupport.CacheOperationCacheKey . |
Constructor and Description |
---|
CacheAspectSupport() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
void |
afterSingletonsInstantiated()
Invoked right at the end of the singleton pre-instantiation phase,
with a guarantee that all regular singleton beans have been created
already.
|
protected void |
clearMetadataCache()
Clear the cached metadata.
|
private void |
collectPutRequests(java.util.Collection<CacheAspectSupport.CacheOperationContext> contexts,
java.lang.Object result,
java.util.Collection<CacheAspectSupport.CachePutRequest> putRequests)
Collect the
CacheAspectSupport.CachePutRequest for all CacheOperation using
the specified result item. |
private java.lang.Object |
execute(CacheOperationInvoker invoker,
CacheAspectSupport.CacheOperationContexts contexts) |
protected java.lang.Object |
execute(CacheOperationInvoker invoker,
java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object[] args) |
private Cache.ValueWrapper |
findCachedItem(java.util.Collection<CacheAspectSupport.CacheOperationContext> contexts)
Find a cached item only for
CacheableOperation that passes the condition. |
private Cache.ValueWrapper |
findInCaches(CacheAspectSupport.CacheOperationContext context,
java.lang.Object key) |
private java.lang.Object |
generateKey(CacheAspectSupport.CacheOperationContext context,
java.lang.Object result) |
protected <T> T |
getBean(java.lang.String beanName,
java.lang.Class<T> expectedType)
Return a bean with the specified name and type.
|
protected CacheAspectSupport.CacheOperationMetadata |
getCacheOperationMetadata(CacheOperation operation,
java.lang.reflect.Method method,
java.lang.Class<?> targetClass)
Return the
CacheAspectSupport.CacheOperationMetadata for the specified operation. |
CacheOperationSource |
getCacheOperationSource()
Return the CacheOperationSource for this cache aspect.
|
CacheResolver |
getCacheResolver()
Return the default
CacheResolver that this cache aspect delegates to. |
protected java.util.Collection<? extends Cache> |
getCaches(CacheOperationInvocationContext<CacheOperation> context,
CacheResolver cacheResolver) |
KeyGenerator |
getKeyGenerator()
Return the default
KeyGenerator that this cache aspect delegates to. |
protected CacheAspectSupport.CacheOperationContext |
getOperationContext(CacheOperation operation,
java.lang.reflect.Method method,
java.lang.Object[] args,
java.lang.Object target,
java.lang.Class<?> targetClass) |
private java.lang.Class<?> |
getTargetClass(java.lang.Object target) |
private boolean |
hasCachePut(CacheAspectSupport.CacheOperationContexts contexts) |
protected java.lang.Object |
invokeOperation(CacheOperationInvoker invoker)
Execute the underlying operation (typically in case of cache miss) and return
the result of the invocation.
|
private boolean |
isConditionPassing(CacheAspectSupport.CacheOperationContext context,
java.lang.Object result) |
private void |
logInvalidating(CacheAspectSupport.CacheOperationContext context,
CacheEvictOperation operation,
java.lang.Object key) |
protected java.lang.String |
methodIdentification(java.lang.reflect.Method method,
java.lang.Class<?> targetClass)
Convenience method to return a String representation of this Method
for use in logging.
|
private void |
performCacheEvict(CacheAspectSupport.CacheOperationContext context,
CacheEvictOperation operation,
java.lang.Object result) |
private void |
processCacheEvicts(java.util.Collection<CacheAspectSupport.CacheOperationContext> contexts,
boolean beforeInvocation,
java.lang.Object result) |
void |
setApplicationContext(ApplicationContext applicationContext)
Set the ApplicationContext that this object runs in.
|
void |
setCacheManager(CacheManager cacheManager)
Set the
CacheManager to use to create a default CacheResolver . |
void |
setCacheOperationSources(CacheOperationSource... cacheOperationSources)
Set one or more cache operation sources which are used to find the cache
attributes.
|
void |
setCacheResolver(CacheResolver cacheResolver)
Set the default
CacheResolver that this cache aspect should delegate
to if no specific cache resolver has been set for the operation. |
void |
setKeyGenerator(KeyGenerator keyGenerator)
Set the default
KeyGenerator that this cache aspect should delegate to
if no specific key generator has been set for the operation. |
doClear, doEvict, doGet, doPut, getErrorHandler, setErrorHandler
protected final Log logger
private final java.util.Map<CacheAspectSupport.CacheOperationCacheKey,CacheAspectSupport.CacheOperationMetadata> metadataCache
CacheAspectSupport.CacheOperationCacheKey
.private final ExpressionEvaluator evaluator
private CacheOperationSource cacheOperationSource
private KeyGenerator keyGenerator
private CacheResolver cacheResolver
private ApplicationContext applicationContext
private boolean initialized
public void setCacheOperationSources(CacheOperationSource... cacheOperationSources)
CompositeCacheOperationSource
.public CacheOperationSource getCacheOperationSource()
public void setKeyGenerator(KeyGenerator keyGenerator)
KeyGenerator
that this cache aspect should delegate to
if no specific key generator has been set for the operation.
The default is a SimpleKeyGenerator
public KeyGenerator getKeyGenerator()
KeyGenerator
that this cache aspect delegates to.public void setCacheManager(CacheManager cacheManager)
CacheManager
to use to create a default CacheResolver
.
Replace the current CacheResolver
, if any.public void setCacheResolver(CacheResolver cacheResolver)
CacheResolver
that this cache aspect should delegate
to if no specific cache resolver has been set for the operation.
The default resolver resolves the caches against their names and the default cache manager.
public CacheResolver getCacheResolver()
CacheResolver
that this cache aspect delegates to.public void setApplicationContext(ApplicationContext applicationContext)
ApplicationContextAware
Invoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and
MessageSourceAware
, if applicable.
setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectBeanInitializationException
public void afterPropertiesSet()
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
public void afterSingletonsInstantiated()
SmartInitializingSingleton
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
calls within
this method won't trigger accidental side effects during bootstrap.
NOTE: This callback won't be triggered for singleton beans
lazily initialized on demand after BeanFactory
bootstrap,
and not for any other bean scope either. Carefully use it for beans
with the intended bootstrap semantics only.
afterSingletonsInstantiated
in interface SmartInitializingSingleton
protected java.lang.String methodIdentification(java.lang.reflect.Method method, java.lang.Class<?> targetClass)
method
- the method we're interested intargetClass
- class the method is onClassUtils.getQualifiedMethodName(java.lang.reflect.Method)
protected java.util.Collection<? extends Cache> getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver)
protected CacheAspectSupport.CacheOperationContext getOperationContext(CacheOperation operation, java.lang.reflect.Method method, java.lang.Object[] args, java.lang.Object target, java.lang.Class<?> targetClass)
protected CacheAspectSupport.CacheOperationMetadata getCacheOperationMetadata(CacheOperation operation, java.lang.reflect.Method method, java.lang.Class<?> targetClass)
CacheAspectSupport.CacheOperationMetadata
for the specified operation.
Resolve the CacheResolver
and the KeyGenerator
to be
used for the operation.
operation
- the operationmethod
- the method on which the operation is invokedtargetClass
- the target typeprotected <T> T getBean(java.lang.String beanName, java.lang.Class<T> expectedType)
CacheOperation
.beanName
- the name of the bean, as defined by the operationexpectedType
- type type for the beanNoSuchBeanDefinitionException
- if such bean does not existCacheOperation.keyGenerator
,
CacheOperation.cacheManager
,
CacheOperation.cacheResolver
protected void clearMetadataCache()
protected java.lang.Object execute(CacheOperationInvoker invoker, java.lang.Object target, java.lang.reflect.Method method, java.lang.Object[] args)
protected java.lang.Object invokeOperation(CacheOperationInvoker invoker)
CacheOperationInvoker.ThrowableWrapper
: the exception can be handled
or modified but it must be wrapped in a
CacheOperationInvoker.ThrowableWrapper
as well.invoker
- the invoker handling the operation being cachedCacheOperationInvoker.invoke()
private java.lang.Class<?> getTargetClass(java.lang.Object target)
private java.lang.Object execute(CacheOperationInvoker invoker, CacheAspectSupport.CacheOperationContexts contexts)
private boolean hasCachePut(CacheAspectSupport.CacheOperationContexts contexts)
private void processCacheEvicts(java.util.Collection<CacheAspectSupport.CacheOperationContext> contexts, boolean beforeInvocation, java.lang.Object result)
private void performCacheEvict(CacheAspectSupport.CacheOperationContext context, CacheEvictOperation operation, java.lang.Object result)
private void logInvalidating(CacheAspectSupport.CacheOperationContext context, CacheEvictOperation operation, java.lang.Object key)
private Cache.ValueWrapper findCachedItem(java.util.Collection<CacheAspectSupport.CacheOperationContext> contexts)
CacheableOperation
that passes the condition.contexts
- the cacheable operationsCache.ValueWrapper
holding the cached item,
or null
if none is foundprivate void collectPutRequests(java.util.Collection<CacheAspectSupport.CacheOperationContext> contexts, java.lang.Object result, java.util.Collection<CacheAspectSupport.CachePutRequest> putRequests)
CacheAspectSupport.CachePutRequest
for all CacheOperation
using
the specified result item.contexts
- the contexts to handleresult
- the result item (never null
)putRequests
- the collection to updateprivate Cache.ValueWrapper findInCaches(CacheAspectSupport.CacheOperationContext context, java.lang.Object key)
private boolean isConditionPassing(CacheAspectSupport.CacheOperationContext context, java.lang.Object result)
private java.lang.Object generateKey(CacheAspectSupport.CacheOperationContext context, java.lang.Object result)