public abstract class CacheAspectSupport extends AbstractCacheInvoker implements BeanFactoryAware, InitializingBean, SmartInitializingSingleton
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).
|
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.
|
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)
Deprecated.
as of 4.3, in favor of
setBeanFactory(org.springframework.beans.factory.BeanFactory) |
void |
setBeanFactory(BeanFactory beanFactory)
Set the containing
BeanFactory for CacheManager and other
service lookups. |
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
.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 setBeanFactory(BeanFactory beanFactory)
BeanFactory
for CacheManager
and other
service lookups.setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
@Deprecated public void setApplicationContext(ApplicationContext applicationContext)
setBeanFactory(org.springframework.beans.factory.BeanFactory)
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 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 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()