public abstract class CacheAspectSupport extends AbstractCacheInvoker implements InitializingBean, 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 |
---|---|
protected class |
CacheAspectSupport.CacheOperationContext |
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.
|
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).
|
protected void |
clearMetadataCache()
Clear the cached metadata.
|
protected Object |
execute(CacheOperationInvoker invoker,
Object target,
Method method,
Object[] args) |
protected <T> T |
getBean(String beanName,
Class<T> expectedType)
Return a bean with the specified name and type.
|
protected CacheAspectSupport.CacheOperationMetadata |
getCacheOperationMetadata(CacheOperation operation,
Method method,
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 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,
Method method,
Object[] args,
Object target,
Class<?> targetClass) |
protected Object |
invokeOperation(CacheOperationInvoker invoker)
Execute the underlying operation (typically in case of cache miss) and return
the result of the invocation.
|
protected String |
methodIdentification(Method method,
Class<?> targetClass)
Convenience method to return a String representation of this Method
for use in logging.
|
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
public void setCacheOperationSources(CacheOperationSource... cacheOperationSources)
CompositeCacheOperationSource
.cacheOperationSources
- must not be null
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
protected String methodIdentification(Method method, Class<?> targetClass)
method
- the method we're interested intargetClass
- class the method is onClassUtils.getQualifiedMethodName(java.lang.reflect.Method)
protected Collection<? extends Cache> getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver)
protected CacheAspectSupport.CacheOperationContext getOperationContext(CacheOperation operation, Method method, Object[] args, Object target, Class<?> targetClass)
protected CacheAspectSupport.CacheOperationMetadata getCacheOperationMetadata(CacheOperation operation, Method method, 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(String beanName, 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 Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args)
protected 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()