Class CacheAspectSupport
- java.lang.Object
- org.springframework.cache.interceptor.AbstractCacheInvoker
- org.springframework.cache.interceptor.CacheAspectSupport
- All Implemented Interfaces:
Aware
,BeanFactoryAware
,InitializingBean
,SmartInitializingSingleton
- Direct Known Subclasses:
CacheInterceptor
public abstract class CacheAspectSupport extends AbstractCacheInvoker implements BeanFactoryAware, InitializingBean, SmartInitializingSingleton
Base class for caching aspects, such as theCacheInterceptor
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, aKeyGenerator
will build the cache keys, and aCacheResolver
will resolve the actual cache(s) to use.Note: A cache aspect is serializable but does not perform any actual caching after deserialization.
- Since:
- 3.1
- Author:
- Costin Leau, Juergen Hoeller, Chris Beams, Phillip Webb, Sam Brannen, Stephane Nicoll
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
CacheAspectSupport.CacheOperationContext
ACacheOperationInvocationContext
context for aCacheOperation
.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 Summary
Constructors Constructor Description CacheAspectSupport()
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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 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 theCacheAspectSupport.CacheOperationMetadata
for the specified operation.CacheOperationSource
getCacheOperationSource()
Return the CacheOperationSource for this cache aspect.CacheResolver
getCacheResolver()
Return the defaultCacheResolver
that this cache aspect delegates to.protected Collection<? extends Cache>
getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver)
KeyGenerator
getKeyGenerator()
Return the defaultKeyGenerator
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 ofsetBeanFactory(org.springframework.beans.factory.BeanFactory)
void
setBeanFactory(BeanFactory beanFactory)
Set the containingBeanFactory
forCacheManager
and other service lookups.void
setCacheManager(CacheManager cacheManager)
Set theCacheManager
to use to create a defaultCacheResolver
.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 defaultCacheResolver
that this cache aspect should delegate to if no specific cache resolver has been set for the operation.void
setKeyGenerator(KeyGenerator keyGenerator)
Set the defaultKeyGenerator
that this cache aspect should delegate to if no specific key generator has been set for the operation.Methods inherited from class org.springframework.cache.interceptor.AbstractCacheInvoker
doClear, doEvict, doGet, doPut, getErrorHandler, setErrorHandler
Constructor Detail
CacheAspectSupport
public CacheAspectSupport()
Method Detail
setCacheOperationSources
public void setCacheOperationSources(CacheOperationSource... cacheOperationSources)
Set one or more cache operation sources which are used to find the cache attributes. If more than one source is provided, they will be aggregated using aCompositeCacheOperationSource
.
getCacheOperationSource
public CacheOperationSource getCacheOperationSource()
Return the CacheOperationSource for this cache aspect.
setKeyGenerator
public void setKeyGenerator(KeyGenerator keyGenerator)
Set the defaultKeyGenerator
that this cache aspect should delegate to if no specific key generator has been set for the operation.The default is a
SimpleKeyGenerator
.
getKeyGenerator
public KeyGenerator getKeyGenerator()
Return the defaultKeyGenerator
that this cache aspect delegates to.
setCacheResolver
public void setCacheResolver(CacheResolver cacheResolver)
Set the defaultCacheResolver
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.
getCacheResolver
public CacheResolver getCacheResolver()
Return the defaultCacheResolver
that this cache aspect delegates to.
setCacheManager
public void setCacheManager(CacheManager cacheManager)
Set theCacheManager
to use to create a defaultCacheResolver
. Replace the currentCacheResolver
, if any.
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
Set the containingBeanFactory
forCacheManager
and other service lookups.- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Parameters:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- Since:
- 4.3
- See Also:
BeanInitializationException
setApplicationContext
@Deprecated public void setApplicationContext(ApplicationContext applicationContext)
Deprecated.as of 4.3, in favor ofsetBeanFactory(org.springframework.beans.factory.BeanFactory)
afterPropertiesSet
public void afterPropertiesSet()
Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
afterSingletonsInstantiated
public void afterSingletonsInstantiated()
Description copied from interface:SmartInitializingSingleton
Invoked right at the end of the singleton pre-instantiation phase, with a guarantee that all regular singleton beans have been created already.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.- Specified by:
afterSingletonsInstantiated
in interfaceSmartInitializingSingleton
methodIdentification
protected String methodIdentification(Method method, Class<?> targetClass)
Convenience method to return a String representation of this Method for use in logging. Can be overridden in subclasses to provide a different identifier for the given method.- Parameters:
method
- the method we're interested intargetClass
- class the method is on- Returns:
- log message identifying this method
- See Also:
ClassUtils.getQualifiedMethodName(java.lang.reflect.Method)
getCaches
protected Collection<? extends Cache> getCaches(CacheOperationInvocationContext<CacheOperation> context, CacheResolver cacheResolver)
getOperationContext
protected CacheAspectSupport.CacheOperationContext getOperationContext(CacheOperation operation, Method method, Object[] args, Object target, Class<?> targetClass)
getCacheOperationMetadata
protected CacheAspectSupport.CacheOperationMetadata getCacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass)
Return theCacheAspectSupport.CacheOperationMetadata
for the specified operation.Resolve the
CacheResolver
and theKeyGenerator
to be used for the operation.- Parameters:
operation
- the operationmethod
- the method on which the operation is invokedtargetClass
- the target type- Returns:
- the resolved metadata for the operation
getBean
protected <T> T getBean(String beanName, Class<T> expectedType)
Return a bean with the specified name and type. Used to resolve services that are referenced by name in aCacheOperation
.- Parameters:
beanName
- the name of the bean, as defined by the operationexpectedType
- type for the bean- Returns:
- the bean matching that name
- Throws:
NoSuchBeanDefinitionException
- if such bean does not exist- See Also:
CacheOperation.getKeyGenerator()
,CacheOperation.getCacheManager()
,CacheOperation.getCacheResolver()
clearMetadataCache
protected void clearMetadataCache()
Clear the cached metadata.
execute
protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args)
invokeOperation
protected Object invokeOperation(CacheOperationInvoker invoker)
Execute the underlying operation (typically in case of cache miss) and return the result of the invocation. If an exception occurs it will be wrapped in aCacheOperationInvoker.ThrowableWrapper
: the exception can be handled or modified but it must be wrapped in aCacheOperationInvoker.ThrowableWrapper
as well.- Parameters:
invoker
- the invoker handling the operation being cached- Returns:
- the result of the invocation
- See Also:
CacheOperationInvoker.invoke()