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 relevant methods in the correct order.
Uses the Strategy design pattern. A CacheOperationSource
is
used for determining caching operations, a KeyGenerator
will build the
cache keys, and a CacheResolver
will resolve the actual cache(s) to use.
Note: A cache aspect is serializable but does not perform any actual caching after deserialization.
Modifier and Type | Class and Description |
---|---|
protected class |
CacheAspectSupport.CacheOperationContext
A
CacheOperationInvocationContext context for a CacheOperation . |
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 the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
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 java.lang.Object |
execute(CacheOperationInvoker invoker,
java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object[] args) |
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) |
protected java.lang.Object |
invokeOperation(CacheOperationInvoker invoker)
Execute the underlying operation (typically in case of cache miss) and return
the result of the invocation.
|
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.
|
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
.@Nullable 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 setCacheResolver(@Nullable 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.
@Nullable public CacheResolver getCacheResolver()
CacheResolver
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 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
public void afterPropertiesSet()
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, ApplicationContextAware
etc.
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
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 for the beanNoSuchBeanDefinitionException
- if such bean does not existCacheOperation.keyGenerator
,
CacheOperation.cacheManager
,
CacheOperation.cacheResolver
protected void clearMetadataCache()
@Nullable 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()