Skip to content

Commit ceea00f

Browse files
bananayongjhoeller
authored andcommitted
Fix cache annotation tip
Even if using cglib proxy mode, annotations on an interface can be recognized. Signed-off-by: Kwangyong Kim <[email protected]>
1 parent 2fb3f99 commit ceea00f

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ public void fooServiceWithInterfaceCglib() {
7373
fooGetSimple(service);
7474
}
7575

76+
@Test
77+
public void barServiceWithCacheableInterfaceCglib() {
78+
this.context = new AnnotationConfigApplicationContext(BarConfigCglib.class);
79+
BarService service = this.context.getBean(BarService.class);
80+
Cache cache = getCache();
81+
82+
Object key = new Object();
83+
assertCacheMiss(key, cache);
84+
85+
Object value = service.getSimple(key);
86+
assertCacheHit(key, value, cache);
87+
}
88+
7689
private void fooGetSimple(FooService service) {
7790
Cache cache = getCache();
7891

@@ -184,6 +197,31 @@ public Object getWithCondition(Object key) {
184197
}
185198
}
186199

200+
@Configuration
201+
@Import(SharedConfig.class)
202+
@EnableCaching(proxyTargetClass = true)
203+
static class BarConfigCglib {
204+
205+
@Bean
206+
public BarService barService() {
207+
return new BarServiceImpl();
208+
}
209+
}
210+
211+
interface BarService {
212+
@Cacheable(cacheNames = "testCache")
213+
Object getSimple(Object key);
214+
}
215+
216+
static class BarServiceImpl implements BarService {
217+
218+
private final AtomicLong counter = new AtomicLong();
219+
220+
@Override
221+
public Object getSimple(Object key) {
222+
return this.counter.getAndIncrement();
223+
}
224+
}
187225

188226
@Configuration
189227
@Import(FooConfig.class)

src/docs/asciidoc/integration.adoc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6226,11 +6226,9 @@ if you need to annotate non-public methods, as it changes the bytecode itself.
62266226
TIP: Spring recommends that you only annotate concrete classes (and methods of concrete
62276227
classes) with the `@Cache{asterisk}` annotation, as opposed to annotating interfaces.
62286228
You certainly can place the `@Cache{asterisk}` annotation on an interface (or an interface
6229-
method), but this works only as you would expect it to if you use interface-based proxies.
6230-
The fact that Java annotations are not inherited from interfaces means that, if you use
6231-
class-based proxies (`proxy-target-class="true"`) or the weaving-based aspect
6232-
(`mode="aspectj"`), the caching settings are not recognized by the proxying and weaving
6233-
infrastructure, and the object is not wrapped in a caching proxy.
6229+
method), but this works only as you would expect it to if you use the proxy mode (`mode="proxy"`).
6230+
If you use the weaving-based aspect (`mode="aspectj"`), the caching settings are not
6231+
recognized by weaving infrastructure.
62346232

62356233
NOTE: In proxy mode (the default), only external method calls coming in through the
62366234
proxy are intercepted. This means that self-invocation (in effect, a method within the

0 commit comments

Comments
 (0)