Browse Source

Support @⁠CacheConfig("myCache") declarations for simplified config

The @⁠CacheConfig section of the reference manual provides an example
that declares @⁠CacheConfig("books").

However, unlike the @⁠Cacheable, @⁠CachePut, and @⁠CacheEvict
annotations, @⁠CacheConfig currently does not have a `value` alias for
the `cacheNames` attribute. Thus, the example in the reference manual
does not compile, and @⁠CacheConfig(cacheNames = "books") would be the
supported way to declare that.

The driving factor for this commit is therefore to provide a simplified
and consistent programming model for users that only need to define
default cache names at the type level (like in the existing example in
the reference manual).

To address that, this commit introduces a `value` alias for
`cacheNames` in @⁠CacheConfig.

See gh-35096
Closes gh-35152

(cherry picked from commit 6091453feb)
pull/35405/head
Sam Brannen 6 months ago
parent
commit
d07ed31fed
  1. 2
      spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineReactiveCachingTests.java
  2. 13
      spring-context/src/main/java/org/springframework/cache/annotation/CacheConfig.java
  3. 2
      spring-context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTests.java
  4. 6
      spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java
  5. 2
      spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java
  6. 2
      spring-context/src/test/java/org/springframework/cache/interceptor/CacheErrorHandlerTests.java
  7. 2
      spring-context/src/test/java/org/springframework/cache/interceptor/CachePutEvaluationTests.java
  8. 2
      spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomizationTests.java

2
spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineReactiveCachingTests.java vendored

@ -129,7 +129,7 @@ class CaffeineReactiveCachingTests { @@ -129,7 +129,7 @@ class CaffeineReactiveCachingTests {
}
@CacheConfig(cacheNames = "first")
@CacheConfig("first")
static class ReactiveCacheableService {
private final AtomicLong counter = new AtomicLong();

13
spring-context/src/main/java/org/springframework/cache/annotation/CacheConfig.java vendored

@ -22,6 +22,8 @@ import java.lang.annotation.Retention; @@ -22,6 +22,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* {@code @CacheConfig} provides a mechanism for sharing common cache-related
* settings at the class level.
@ -39,6 +41,15 @@ import java.lang.annotation.Target; @@ -39,6 +41,15 @@ import java.lang.annotation.Target;
@Documented
public @interface CacheConfig {
/**
* Alias for {@link #cacheNames}.
* <p>Intended to be used when no other attributes are needed, for example:
* {@code @CacheConfig("books")}.
* @since 6.2.9
*/
@AliasFor("cacheNames")
String[] value() default {};
/**
* Names of the default caches to consider for caching operations defined
* in the annotated class.
@ -47,7 +58,9 @@ public @interface CacheConfig { @@ -47,7 +58,9 @@ public @interface CacheConfig {
* configured {@link #cacheResolver()} which typically delegates to
* {@link org.springframework.cache.CacheManager#getCache}.
* For further details see {@link Cacheable#cacheNames()}.
* @see #value
*/
@AliasFor("value")
String[] cacheNames() default {};
/**

2
spring-context/src/test/java/org/springframework/cache/annotation/AnnotationCacheOperationSourceTests.java vendored

@ -443,7 +443,7 @@ class AnnotationCacheOperationSourceTests { @@ -443,7 +443,7 @@ class AnnotationCacheOperationSourceTests {
}
@CacheConfig(cacheNames = "myCache")
@CacheConfig("myCache")
private interface CacheConfigIfc {
@Cacheable

6
spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java vendored

@ -259,7 +259,7 @@ class ReactiveCachingTests { @@ -259,7 +259,7 @@ class ReactiveCachingTests {
}
@CacheConfig(cacheNames = "first")
@CacheConfig("first")
static class ReactiveCacheableService {
private final AtomicLong counter = new AtomicLong();
@ -285,7 +285,7 @@ class ReactiveCachingTests { @@ -285,7 +285,7 @@ class ReactiveCachingTests {
}
@CacheConfig(cacheNames = "first")
@CacheConfig("first")
static class ReactiveSyncCacheableService {
private final AtomicLong counter = new AtomicLong();
@ -307,7 +307,7 @@ class ReactiveCachingTests { @@ -307,7 +307,7 @@ class ReactiveCachingTests {
}
@CacheConfig(cacheNames = "first")
@CacheConfig("first")
static class ReactiveFailureCacheableService {
private final AtomicBoolean cacheFutureInvoked = new AtomicBoolean();

2
spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java vendored

@ -200,7 +200,7 @@ class EnableCachingIntegrationTests { @@ -200,7 +200,7 @@ class EnableCachingIntegrationTests {
}
@CacheConfig(cacheNames = "testCache")
@CacheConfig("testCache")
static class FooServiceImpl implements FooService {
private final AtomicLong counter = new AtomicLong();

2
spring-context/src/test/java/org/springframework/cache/interceptor/CacheErrorHandlerTests.java vendored

@ -261,7 +261,7 @@ class CacheErrorHandlerTests { @@ -261,7 +261,7 @@ class CacheErrorHandlerTests {
}
@CacheConfig(cacheNames = "test")
@CacheConfig("test")
public static class SimpleService {
private AtomicLong counter = new AtomicLong();

2
spring-context/src/test/java/org/springframework/cache/interceptor/CachePutEvaluationTests.java vendored

@ -121,7 +121,7 @@ class CachePutEvaluationTests { @@ -121,7 +121,7 @@ class CachePutEvaluationTests {
}
@CacheConfig(cacheNames = "test")
@CacheConfig("test")
public static class SimpleService {
private AtomicLong counter = new AtomicLong();

2
spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomizationTests.java vendored

@ -206,7 +206,7 @@ class CacheResolverCustomizationTests { @@ -206,7 +206,7 @@ class CacheResolverCustomizationTests {
}
@CacheConfig(cacheNames = "default")
@CacheConfig("default")
static class SimpleService {
private final AtomicLong counter = new AtomicLong();

Loading…
Cancel
Save