org.springframework.cache.interceptor
Class CacheAspectSupport

java.lang.Object
  extended by org.springframework.cache.interceptor.CacheAspectSupport
All Implemented Interfaces:
InitializingBean
Direct Known Subclasses:
CacheInterceptor

public abstract class CacheAspectSupport
extends java.lang.Object
implements InitializingBean

Base class for caching aspects, such as the 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 CacheManager implementation will perform the actual cache management, and a CacheOperationSource is used for determining caching operations.

A cache aspect is serializable if its CacheManager and CacheOperationSource are serializable.

Since:
3.1
Author:
Costin Leau, Juergen Hoeller, Chris Beams

Nested Class Summary
protected  class CacheAspectSupport.CacheOperationContext
           
private static class CacheAspectSupport.CacheStatus
           
static interface CacheAspectSupport.Invoker
           
 
Field Summary
private static java.lang.String CACHEABLE
           
private  CacheManager cacheManager
           
private  CacheOperationSource cacheOperationSource
           
private  ExpressionEvaluator evaluator
           
private static java.lang.String EVICT
           
private  boolean initialized
           
private  KeyGenerator keyGenerator
           
protected  Log logger
           
private static java.lang.String UPDATE
           
 
Constructor Summary
CacheAspectSupport()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
private  java.util.Map<java.lang.String,java.util.Collection<CacheAspectSupport.CacheOperationContext>> createOperationContext(java.util.Collection<CacheOperation> cacheOp, java.lang.reflect.Method method, java.lang.Object[] args, java.lang.Object target, java.lang.Class<?> targetClass)
           
protected  java.lang.Object execute(CacheAspectSupport.Invoker invoker, java.lang.Object target, java.lang.reflect.Method method, java.lang.Object[] args)
           
 CacheManager getCacheManager()
          Return the CacheManager that this cache aspect delegates to.
 CacheOperationSource getCacheOperationSource()
          Return the CacheOperationSource for this cache aspect.
protected  java.util.Collection<Cache> getCaches(CacheOperation operation)
           
 KeyGenerator getKeyGenerator()
          Return the KeyGenerator for this cache aspect,
protected  CacheAspectSupport.CacheOperationContext getOperationContext(CacheOperation operation, java.lang.reflect.Method method, java.lang.Object[] args, java.lang.Object target, java.lang.Class<?> targetClass)
           
private  CacheAspectSupport.CacheStatus inspectCacheables(java.util.Collection<CacheAspectSupport.CacheOperationContext> cacheables)
           
private  void inspectCacheEvicts(java.util.Collection<CacheAspectSupport.CacheOperationContext> evictions)
           
private  java.util.Map<CacheAspectSupport.CacheOperationContext,java.lang.Object> inspectCacheUpdates(java.util.Collection<CacheAspectSupport.CacheOperationContext> updates)
           
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 setCacheManager(CacheManager cacheManager)
          Set the CacheManager that this cache aspect should delegate to.
 void setCacheOperationSources(CacheOperationSource... cacheOperationSources)
          Set one or more cache operation sources which are used to find the cache attributes.
 void setKeyGenerator(KeyGenerator keyGenerator)
          Set the KeyGenerator for this cache aspect.
private  void update(java.util.Map<CacheAspectSupport.CacheOperationContext,java.lang.Object> updates, java.lang.Object retVal)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger

cacheManager

private CacheManager cacheManager

cacheOperationSource

private CacheOperationSource cacheOperationSource

evaluator

private final ExpressionEvaluator evaluator

keyGenerator

private KeyGenerator keyGenerator

initialized

private boolean initialized

CACHEABLE

private static final java.lang.String CACHEABLE
See Also:
Constant Field Values

UPDATE

private static final java.lang.String UPDATE
See Also:
Constant Field Values

EVICT

private static final java.lang.String EVICT
See Also:
Constant Field Values
Constructor Detail

CacheAspectSupport

public CacheAspectSupport()
Method Detail

setCacheManager

public void setCacheManager(CacheManager cacheManager)
Set the CacheManager that this cache aspect should delegate to.


getCacheManager

public CacheManager getCacheManager()
Return the CacheManager that this cache aspect delegates to.


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 a CompositeCacheOperationSource.

Parameters:
cacheOperationSources - must not be null

getCacheOperationSource

public CacheOperationSource getCacheOperationSource()
Return the CacheOperationSource for this cache aspect.


setKeyGenerator

public void setKeyGenerator(KeyGenerator keyGenerator)
Set the KeyGenerator for this cache aspect. Default is DefaultKeyGenerator.


getKeyGenerator

public KeyGenerator getKeyGenerator()
Return the KeyGenerator for this cache aspect,


afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

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.

Specified by:
afterPropertiesSet in interface InitializingBean

methodIdentification

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. Can be overridden in subclasses to provide a different identifier for the given method.

Parameters:
method - the method we're interested in
targetClass - class the method is on
Returns:
log message identifying this method
See Also:
ClassUtils.getQualifiedMethodName(java.lang.reflect.Method)

getCaches

protected java.util.Collection<Cache> getCaches(CacheOperation operation)

getOperationContext

protected CacheAspectSupport.CacheOperationContext getOperationContext(CacheOperation operation,
                                                                       java.lang.reflect.Method method,
                                                                       java.lang.Object[] args,
                                                                       java.lang.Object target,
                                                                       java.lang.Class<?> targetClass)

execute

protected java.lang.Object execute(CacheAspectSupport.Invoker invoker,
                                   java.lang.Object target,
                                   java.lang.reflect.Method method,
                                   java.lang.Object[] args)

inspectCacheEvicts

private void inspectCacheEvicts(java.util.Collection<CacheAspectSupport.CacheOperationContext> evictions)

inspectCacheables

private CacheAspectSupport.CacheStatus inspectCacheables(java.util.Collection<CacheAspectSupport.CacheOperationContext> cacheables)

inspectCacheUpdates

private java.util.Map<CacheAspectSupport.CacheOperationContext,java.lang.Object> inspectCacheUpdates(java.util.Collection<CacheAspectSupport.CacheOperationContext> updates)

update

private void update(java.util.Map<CacheAspectSupport.CacheOperationContext,java.lang.Object> updates,
                    java.lang.Object retVal)

createOperationContext

private java.util.Map<java.lang.String,java.util.Collection<CacheAspectSupport.CacheOperationContext>> createOperationContext(java.util.Collection<CacheOperation> cacheOp,
                                                                                                                              java.lang.reflect.Method method,
                                                                                                                              java.lang.Object[] args,
                                                                                                                              java.lang.Object target,
                                                                                                                              java.lang.Class<?> targetClass)