Skip to content

Commit d07ed31

Browse files
committed
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 6091453)
1 parent 75b7f5e commit d07ed31

File tree

8 files changed

+22
-9
lines changed

8 files changed

+22
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void fluxCacheDoesntDependOnFirstRequest(Class<?> configClass) {
129129
}
130130

131131

132-
@CacheConfig(cacheNames = "first")
132+
@CacheConfig("first")
133133
static class ReactiveCacheableService {
134134

135135
private final AtomicLong counter = new AtomicLong();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25+
import org.springframework.core.annotation.AliasFor;
26+
2527
/**
2628
* {@code @CacheConfig} provides a mechanism for sharing common cache-related
2729
* settings at the class level.
@@ -39,6 +41,15 @@
3941
@Documented
4042
public @interface CacheConfig {
4143

44+
/**
45+
* Alias for {@link #cacheNames}.
46+
* <p>Intended to be used when no other attributes are needed, for example:
47+
* {@code @CacheConfig("books")}.
48+
* @since 6.2.9
49+
*/
50+
@AliasFor("cacheNames")
51+
String[] value() default {};
52+
4253
/**
4354
* Names of the default caches to consider for caching operations defined
4455
* in the annotated class.
@@ -47,7 +58,9 @@
4758
* configured {@link #cacheResolver()} which typically delegates to
4859
* {@link org.springframework.cache.CacheManager#getCache}.
4960
* For further details see {@link Cacheable#cacheNames()}.
61+
* @see #value
5062
*/
63+
@AliasFor("value")
5164
String[] cacheNames() default {};
5265

5366
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public void multipleCacheConfig() {
443443
}
444444

445445

446-
@CacheConfig(cacheNames = "myCache")
446+
@CacheConfig("myCache")
447447
private interface CacheConfigIfc {
448448

449449
@Cacheable

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void cacheErrorHandlerWithLoggingCacheErrorHandlerAndOperationExceptionAndSync()
259259
}
260260

261261

262-
@CacheConfig(cacheNames = "first")
262+
@CacheConfig("first")
263263
static class ReactiveCacheableService {
264264

265265
private final AtomicLong counter = new AtomicLong();
@@ -285,7 +285,7 @@ Flux<Long> cacheFlux(Object arg) {
285285
}
286286

287287

288-
@CacheConfig(cacheNames = "first")
288+
@CacheConfig("first")
289289
static class ReactiveSyncCacheableService {
290290

291291
private final AtomicLong counter = new AtomicLong();
@@ -307,7 +307,7 @@ Flux<Long> cacheFlux(Object arg) {
307307
}
308308

309309

310-
@CacheConfig(cacheNames = "first")
310+
@CacheConfig("first")
311311
static class ReactiveFailureCacheableService {
312312

313313
private final AtomicBoolean cacheFutureInvoked = new AtomicBoolean();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ interface FooService {
200200
}
201201

202202

203-
@CacheConfig(cacheNames = "testCache")
203+
@CacheConfig("testCache")
204204
static class FooServiceImpl implements FooService {
205205

206206
private final AtomicLong counter = new AtomicLong();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public Cache mockCache() {
261261

262262
}
263263

264-
@CacheConfig(cacheNames = "test")
264+
@CacheConfig("test")
265265
public static class SimpleService {
266266
private AtomicLong counter = new AtomicLong();
267267

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public SimpleService simpleService() {
121121

122122
}
123123

124-
@CacheConfig(cacheNames = "test")
124+
@CacheConfig("test")
125125
public static class SimpleService {
126126
private AtomicLong counter = new AtomicLong();
127127

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public SimpleService simpleService() {
206206
}
207207

208208

209-
@CacheConfig(cacheNames = "default")
209+
@CacheConfig("default")
210210
static class SimpleService {
211211

212212
private final AtomicLong counter = new AtomicLong();

0 commit comments

Comments
 (0)