Package org.springframework.cache.annotation

# Class AnnotationCacheOperationSource

java.lang.Object
org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource
org.springframework.cache.annotation.AnnotationCacheOperationSource

All Implemented Interfaces:
  `Serializable`, `CacheOperationSource`

public class AnnotationCacheOperationSource extends AbstractFallbackCacheOperationSource implements Serializable
Implementation of the `CacheOperationSource` interface for working with caching metadata in annotation format.

This class reads Spring's `Cacheable`, `CachePut` and `CacheEvict` annotations and exposes corresponding caching operation definition to Spring's cache infrastructure. This class may also serve as base class for a custom `CacheOperationSource`.

Since:
  3.1
Author:
  Costin Leau, Juergen Hoeller, Stephane Nicoll
See Also:
  - Serialized Form

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`protected static interface`
`AnnotationCacheOperationSource.CacheOperationProvider`
Callback interface providing `CacheOperation` instance(s) based on a given `CacheAnnotationParser`.

## Field Summary

### Fields inherited from class org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource

`logger`

## Constructor Summary

Constructors
Constructor
Description
`AnnotationCacheOperationSource()`
Create a default AnnotationCacheOperationSource, supporting public methods that carry the `Cacheable` and `CacheEvict` annotations.
`AnnotationCacheOperationSource(boolean publicMethodsOnly)`
Create a default `AnnotationCacheOperationSource`, supporting public methods that carry the `Cacheable` and `CacheEvict` annotations.
`AnnotationCacheOperationSource(Set<CacheAnnotationParser> annotationParsers)`
Create a custom AnnotationCacheOperationSource.
`AnnotationCacheOperationSource(CacheAnnotationParser annotationParser)`
Create a custom AnnotationCacheOperationSource.
`AnnotationCacheOperationSource(CacheAnnotationParser... annotationParsers)`
Create a custom AnnotationCacheOperationSource.

## Method Summary

Modifier and Type
Method
Description
`protected boolean`
`allowPublicMethodsOnly()`
By default, only public methods can be made cacheable.
`protected Collection<CacheOperation>`
`determineCacheOperations(AnnotationCacheOperationSource.CacheOperationProvider provider)`
Determine the cache operation(s) for the given `AnnotationCacheOperationSource.CacheOperationProvider`.
`boolean`
`equals(Object other)`
`protected Collection<CacheOperation>`
`findCacheOperations(Class<?> clazz)`
Subclasses need to implement this to return the cache operations for the given class, if any.
`protected Collection<CacheOperation>`
`findCacheOperations(Method method)`
Subclasses need to implement this to return the cache operations for the given method, if any.
`int`
`hashCode()`
`boolean`
`isCandidateClass(Class<?> targetClass)`
Determine whether the given class is a candidate for cache operations in the metadata format of this `CacheOperationSource`.
`void`
`setPublicMethodsOnly(boolean publicMethodsOnly)`
Set whether cacheable methods are expected to be public.

### Methods inherited from class org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource

`getCacheKey, getCacheOperations, hasCacheOperations`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### AnnotationCacheOperationSource

public AnnotationCacheOperationSource()
Create a default AnnotationCacheOperationSource, supporting public methods that carry the `Cacheable` and `CacheEvict` annotations.

### AnnotationCacheOperationSource

public AnnotationCacheOperationSource(boolean publicMethodsOnly)
Create a default `AnnotationCacheOperationSource`, supporting public methods that carry the `Cacheable` and `CacheEvict` annotations.

Parameters:
  `publicMethodsOnly` - whether to support only annotated public methods typically for use with proxy-based AOP), or protected/private methods as well (typically used with AspectJ class weaving)
See Also:
  - `setPublicMethodsOnly(boolean)`

### AnnotationCacheOperationSource

public AnnotationCacheOperationSource(CacheAnnotationParser annotationParser)
Create a custom AnnotationCacheOperationSource.

Parameters:
  `annotationParser` - the CacheAnnotationParser to use

### AnnotationCacheOperationSource

public AnnotationCacheOperationSource(CacheAnnotationParser... annotationParsers)
Create a custom AnnotationCacheOperationSource.

Parameters:
  `annotationParsers` - the CacheAnnotationParser to use

### AnnotationCacheOperationSource

public AnnotationCacheOperationSource(Set<CacheAnnotationParser> annotationParsers)
Create a custom AnnotationCacheOperationSource.

Parameters:
  `annotationParsers` - the CacheAnnotationParser to use

## Method Details

### setPublicMethodsOnly

public void setPublicMethodsOnly(boolean publicMethodsOnly)
Set whether cacheable methods are expected to be public.

The default is `true`.

Since:
  6.2

### isCandidateClass

public boolean isCandidateClass(Class<?> targetClass)
Description copied from interface: `CacheOperationSource`
Determine whether the given class is a candidate for cache operations in the metadata format of this `CacheOperationSource`.

If this method returns `false`, the methods on the given class will not get traversed for `CacheOperationSource.getCacheOperations(java.lang.reflect.Method, java.lang.Class<?>)` introspection. Returning `false` is therefore an optimization for non-affected classes, whereas `true` simply means that the class needs to get fully introspected for each method on the given class individually.

Specified by:
  `isCandidateClass` in interface `CacheOperationSource`
Parameters:
  `targetClass` - the class to introspect
Returns:
  `false` if the class is known to have no cache operation metadata at class or method level; `true` otherwise. The default implementation returns `true`, leading to regular introspection.
See Also:
  - `CacheOperationSource.hasCacheOperations(java.lang.reflect.Method, java.lang.Class<?>)`

### findCacheOperations

@Nullable protected Collection<CacheOperation> findCacheOperations(Class<?> clazz)
Description copied from class: `AbstractFallbackCacheOperationSource`
Subclasses need to implement this to return the cache operations for the given class, if any.

Specified by:
  `findCacheOperations` in class `AbstractFallbackCacheOperationSource`
Parameters:
  `clazz` - the class to retrieve the cache operations for
Returns:
  all cache operations associated with this class, or `null` if none

### findCacheOperations

@Nullable protected Collection<CacheOperation> findCacheOperations(Method method)
Description copied from class: `AbstractFallbackCacheOperationSource`
Subclasses need to implement this to return the cache operations for the given method, if any.

Specified by:
  `findCacheOperations` in class `AbstractFallbackCacheOperationSource`
Parameters:
  `method` - the method to retrieve the cache operations for
Returns:
  all cache operations associated with this method, or `null` if none

### determineCacheOperations

@Nullable protected Collection<CacheOperation> determineCacheOperations(AnnotationCacheOperationSource.CacheOperationProvider provider)
Determine the cache operation(s) for the given `AnnotationCacheOperationSource.CacheOperationProvider`.

This implementation delegates to configured `CacheAnnotationParsers` for parsing known annotations into Spring's metadata attribute class.

Can be overridden to support custom annotations that carry caching metadata.

Parameters:
  `provider` - the cache operation provider to use
Returns:
  the configured caching operations, or `null` if none found

### allowPublicMethodsOnly

protected boolean allowPublicMethodsOnly()
By default, only public methods can be made cacheable.

Overrides:
  `allowPublicMethodsOnly` in class `AbstractFallbackCacheOperationSource`
See Also:
  - `setPublicMethodsOnly(boolean)`

### equals

public boolean equals(@Nullable Object other)

Overrides:
  `equals` in class `Object`

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `Object`