Skip to content

Commit 3cca57a

Browse files
committed
Local merged annotation lookup in SpringCacheAnnotationParser
Issue: SPR-14781 Issue: SPR-14801 (cherry picked from commit 08972ef)
1 parent 5578a2e commit 3cca57a

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,28 @@ public Collection<CacheOperation> parseCacheAnnotations(Method method) {
6262
protected Collection<CacheOperation> parseCacheAnnotations(DefaultCacheConfig cachingConfig, AnnotatedElement ae) {
6363
Collection<CacheOperation> ops = null;
6464

65-
Collection<Cacheable> cacheables = AnnotatedElementUtils.findAllMergedAnnotations(ae, Cacheable.class);
65+
Collection<Cacheable> cacheables = AnnotatedElementUtils.getAllMergedAnnotations(ae, Cacheable.class);
6666
if (!cacheables.isEmpty()) {
6767
ops = lazyInit(ops);
6868
for (Cacheable cacheable : cacheables) {
6969
ops.add(parseCacheableAnnotation(ae, cachingConfig, cacheable));
7070
}
7171
}
72-
Collection<CacheEvict> evicts = AnnotatedElementUtils.findAllMergedAnnotations(ae, CacheEvict.class);
72+
Collection<CacheEvict> evicts = AnnotatedElementUtils.getAllMergedAnnotations(ae, CacheEvict.class);
7373
if (!evicts.isEmpty()) {
7474
ops = lazyInit(ops);
7575
for (CacheEvict evict : evicts) {
7676
ops.add(parseEvictAnnotation(ae, cachingConfig, evict));
7777
}
7878
}
79-
Collection<CachePut> puts = AnnotatedElementUtils.findAllMergedAnnotations(ae, CachePut.class);
79+
Collection<CachePut> puts = AnnotatedElementUtils.getAllMergedAnnotations(ae, CachePut.class);
8080
if (!puts.isEmpty()) {
8181
ops = lazyInit(ops);
8282
for (CachePut put : puts) {
8383
ops.add(parsePutAnnotation(ae, cachingConfig, put));
8484
}
8585
}
86-
Collection<Caching> cachings = AnnotatedElementUtils.findAllMergedAnnotations(ae, Caching.class);
86+
Collection<Caching> cachings = AnnotatedElementUtils.getAllMergedAnnotations(ae, Caching.class);
8787
if (!cachings.isEmpty()) {
8888
ops = lazyInit(ops);
8989
for (Caching caching : cachings) {

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

+47-7
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void caching() throws Exception {
7575

7676
@Test
7777
public void emptyCaching() throws Exception {
78-
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "emptyCaching", 0);
78+
getOps(AnnotatedClass.class, "emptyCaching", 0);
7979
}
8080

8181
@Test
@@ -155,7 +155,7 @@ public void customKeyGeneratorInherited() {
155155

156156
@Test
157157
public void keyAndKeyGeneratorCannotBeSetTogether() {
158-
exception.expect(IllegalStateException.class);
158+
this.exception.expect(IllegalStateException.class);
159159
getOps(AnnotatedClass.class, "invalidKeyAndKeyGeneratorSet");
160160
}
161161

@@ -189,7 +189,7 @@ public void customCacheResolverInherited() {
189189

190190
@Test
191191
public void cacheResolverAndCacheManagerCannotBeSetTogether() {
192-
exception.expect(IllegalStateException.class);
192+
this.exception.expect(IllegalStateException.class);
193193
getOps(AnnotatedClass.class, "invalidCacheResolverAndCacheManagerSet");
194194
}
195195

@@ -244,6 +244,22 @@ public void severalCacheConfigUseClosest() {
244244
assertSharedConfig(cacheOperation, "", "", "", "myCache");
245245
}
246246

247+
@Test
248+
public void cacheConfigFromInterface() {
249+
assertNull(getOps(InterfaceCacheConfig.class, "interfaceCacheConfig"));
250+
Collection<CacheOperation> ops = getOps(CacheConfigIfc.class, "interfaceCacheConfig");
251+
CacheOperation cacheOperation = ops.iterator().next();
252+
assertSharedConfig(cacheOperation, "", "", "", "myCache");
253+
}
254+
255+
@Test
256+
public void cacheAnnotationOverride() {
257+
Collection<CacheOperation> ops = getOps(InterfaceCacheConfig.class, "interfaceCacheableOverride");
258+
assertSame(1, ops.size());
259+
CacheOperation cacheOperation = ops.iterator().next();
260+
assertTrue(cacheOperation instanceof CacheableOperation);
261+
}
262+
247263
@Test
248264
public void partialClassLevelWithCustomCacheManager() {
249265
Collection<CacheOperation> ops = getOps(AnnotatedClassWithSomeDefault.class, "methodLevelCacheManager", 1);
@@ -275,7 +291,7 @@ private Collection<CacheOperation> getOps(Class<?> target, String name, int expe
275291
private Collection<CacheOperation> getOps(Class<?> target, String name) {
276292
try {
277293
Method method = target.getMethod(name);
278-
return source.getCacheOperations(method, target);
294+
return this.source.getCacheOperations(method, target);
279295
}
280296
catch (NoSuchMethodException ex) {
281297
throw new IllegalStateException(ex);
@@ -430,7 +446,7 @@ public void noCustomization() {
430446

431447

432448
@CacheConfigFoo
433-
@CacheConfig(cacheNames = "myCache") // multiple sources
449+
@CacheConfig(cacheNames = "myCache") // multiple sources
434450
private static class MultipleCacheConfig {
435451

436452
@Cacheable
@@ -439,6 +455,30 @@ public void multipleCacheConfig() {
439455
}
440456

441457

458+
@CacheConfig(cacheNames = "myCache")
459+
private interface CacheConfigIfc {
460+
461+
@Cacheable
462+
void interfaceCacheConfig();
463+
464+
@CachePut
465+
void interfaceCacheableOverride();
466+
}
467+
468+
469+
private static class InterfaceCacheConfig implements CacheConfigIfc {
470+
471+
@Override
472+
public void interfaceCacheConfig() {
473+
}
474+
475+
@Override
476+
@Cacheable
477+
public void interfaceCacheableOverride() {
478+
}
479+
}
480+
481+
442482
@Retention(RetentionPolicy.RUNTIME)
443483
@Target(ElementType.METHOD)
444484
@Cacheable("foo")
@@ -491,7 +531,7 @@ public void multipleCacheConfig() {
491531

492532

493533
@Retention(RetentionPolicy.RUNTIME)
494-
@Target({ ElementType.METHOD, ElementType.TYPE })
534+
@Target({ElementType.METHOD, ElementType.TYPE})
495535
@Cacheable(cacheNames = "shadowed cache name", key = "shadowed key")
496536
@interface ComposedCacheable {
497537

@@ -507,7 +547,7 @@ public void multipleCacheConfig() {
507547

508548

509549
@Retention(RetentionPolicy.RUNTIME)
510-
@Target({ ElementType.METHOD, ElementType.TYPE })
550+
@Target({ElementType.METHOD, ElementType.TYPE})
511551
@CacheEvict(cacheNames = "shadowed cache name", key = "shadowed key")
512552
@interface ComposedCacheEvict {
513553

0 commit comments

Comments
 (0)