Top Description Inners Fields Constructors Methods
org.springframework.cache.interceptor

public abstract Class CacheAspectSupport

extends AbstractCacheInvoker
implements BeanFactoryAware, InitializingBean, SmartInitializingSingleton
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.SmartInitializingSingleton, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware
Known Direct Subclasses
org.springframework.cache.interceptor.CacheInterceptor
Imports
java.lang.reflect.Method, .Proxy, java.util.ArrayList, .Collection, .Collections, .List, .Map, .Optional, java.util.concurrent.CompletableFuture, .ConcurrentHashMap, java.util.function.Supplier, org.apache.commons.logging.Log, .LogFactory, org.reactivestreams.Subscriber, .Subscription, reactor.core.publisher.Flux, .Mono, org.springframework.aop.framework.AopProxyUtils, org.springframework.aop.support.AopUtils, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .InitializingBean, .NoSuchBeanDefinitionException, .NoUniqueBeanDefinitionException, .SmartInitializingSingleton, org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils, org.springframework.cache.Cache, .CacheManager, org.springframework.context.expression.AnnotatedElementKey, .BeanFactoryResolver, org.springframework.core.BridgeMethodResolver, .KotlinDetector, .ReactiveAdapter, .ReactiveAdapterRegistry, .SpringProperties, org.springframework.expression.EvaluationContext, org.springframework.expression.spel.support.StandardEvaluationContext, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .CollectionUtils, .LinkedMultiValueMap, .MultiValueMap, .ObjectUtils, .ReflectionUtils, .StringUtils, org.springframework.util.function.SingletonSupplier, .SupplierUtils

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

Authors
Costin Leau, Juergen Hoeller, Chris Beams, Phillip Webb, Sam Brannen, Stephane Nicoll, Sebastien Deleuze
Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
protected class
private class
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.

private class
CacheAspectSupport.CachePutListSubscriber

Reactive Streams Subscriber collection for collecting a List to cache.

private class
private class
CacheAspectSupport.ReactiveCachingHandler

Inner class to avoid a hard dependency on the Reactive Streams API at runtime.

Field Summary

Modifier and TypeField and Description
private BeanFactory
private CacheOperationSource
private SingletonSupplier<CacheResolver>
private final CacheOperationExpressionEvaluator
public static final String
IGNORE_REACTIVESTREAMS_PROPERTY_NAME

System property that instructs Spring's caching infrastructure to ignore the presence of Reactive Streams, in particular Reactor's Mono/Flux in org.springframework.cache.annotation.Cacheable method return type declarations.

private boolean
private SingletonSupplier<KeyGenerator>
protected final Log
private final Map<CacheAspectSupport.CacheOperationCacheKey, CacheAspectSupport.CacheOperationMetadata>
private final StandardEvaluationContext
private final CacheAspectSupport.ReactiveCachingHandler
private static final boolean
private static final boolean
Inherited from org.springframework.cache.interceptor.AbstractCacheInvoker:
errorHandler

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing 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.

public void
afterSingletonsInstantiated()

Implements org.springframework.beans.factory.SmartInitializingSingleton.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.

private void
collectPutRequests(Collection<CacheAspectSupport.CacheOperationContext>
the contexts to handle
contexts
,
Object
the result value
result
,
Collection<CacheAspectSupport.CachePutRequest>
the collection to update
putRequests
)

Collect a CachePutRequest for every CacheOperation using the specified result value.

public void
configure(Supplier<CacheErrorHandler> errorHandler, Supplier<KeyGenerator> keyGenerator, Supplier<CacheResolver> cacheResolver, Supplier<CacheManager> cacheManager)

Configure this aspect with the given error handler, key generator and cache resolver/manager suppliers, applying the corresponding default if a supplier is not resolvable.

private Object
protected Object
execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args)

private Object
private Object
private Object

Returns:

a Cache.ValueWrapper holding the cached value, or null if none is found
findCachedValue
(CacheOperationInvoker invoker, Method method, CacheAspectSupport.CacheOperationContexts
the cacheable operations
contexts
)

Find a cached value only for CacheableOperation that passes the condition.

private Object
private Object
protected <T> T

Returns:

the bean matching the expected type, qualified by the given name
getBean
(String
the name of the bean, as defined by the cache operation
name
,
Class<T>
the type expected by the operation's service reference
serviceType
)

Retrieve a bean with the specified name and type.

protected CacheAspectSupport.CacheOperationMetadata

Returns:

the resolved metadata for the operation
getCacheOperationMetadata
(CacheOperation
the operation
operation
,
Method
the method on which the operation is invoked
method
,
Class<?>
the target type
targetClass
)

Return the CacheOperationMetadata for the specified operation.

public CacheOperationSource
getCacheOperationSource()

Return the CacheOperationSource for this cache aspect.

public CacheResolver
getCacheResolver()

Return the default CacheResolver that this cache aspect delegates to.

protected Collection<? extends Cache>
public 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)

private boolean
protected Object

Returns:

the result of the invocation
invokeOperation
(CacheOperationInvoker
the invoker handling the operation being cached
invoker
)

Execute the underlying operation (typically in case of cache miss) and return the result of the invocation.

private boolean
private void
protected String

Returns:

log message identifying this method
methodIdentification
(Method
the method we're interested in
method
,
Class<?>
class the method is on
targetClass
)

Convenience method to return a String representation of this Method for use in logging.

private void
private Object
processCacheEvicts(Collection<CacheAspectSupport.CacheOperationContext> contexts, boolean beforeInvocation, Object result)

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Set the containing BeanFactory for CacheManager and other service lookups.
public void
setCacheManager(CacheManager cacheManager)

Set the CacheManager to use to create a default CacheResolver.

public void
setCacheOperationSource(CacheOperationSource cacheOperationSource)

Set the CacheOperationSource for this cache aspect.

public void
setCacheOperationSources(CacheOperationSource... cacheOperationSources)

Set one or more cache operation sources which are used to find the cache attributes.

public 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.

public 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.

private Object
private Object
private Object
wrapCacheValue(Method method, Object cacheValue)

Inherited from org.springframework.cache.interceptor.AbstractCacheInvoker:
doCleardoEvictdoGetdoPutgetErrorHandlersetErrorHandler