Skip to content

Commit d53ede9

Browse files
committed
CacheProxyFactoryBean exposes all relevant CacheInterceptor callbacks
Issue: SPR-16295 (cherry picked from commit b160f93)
1 parent 092b3d4 commit d53ede9

File tree

3 files changed

+208
-31
lines changed

3 files changed

+208
-31
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,22 +53,20 @@
5353
import org.springframework.util.StringUtils;
5454

5555
/**
56-
* Base class for caching aspects, such as the {@link CacheInterceptor}
57-
* or an AspectJ aspect.
56+
* Base class for caching aspects, such as the {@link CacheInterceptor} or an
57+
* AspectJ aspect.
5858
*
59-
* <p>This enables the underlying Spring caching infrastructure to be
60-
* used easily to implement an aspect for any aspect system.
59+
* <p>This enables the underlying Spring caching infrastructure to be used easily
60+
* to implement an aspect for any aspect system.
6161
*
62-
* <p>Subclasses are responsible for calling methods in this class in
63-
* the correct order.
62+
* <p>Subclasses are responsible for calling relevant methods in the correct order.
6463
*
65-
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheResolver}
66-
* implementation will resolve the actual cache(s) to use, and a
67-
* {@link CacheOperationSource} is used for determining caching
68-
* operations.
64+
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheOperationSource} is
65+
* used for determining caching operations, a {@link KeyGenerator} will build the
66+
* cache keys, and a {@link CacheResolver} will resolve the actual cache(s) to use.
6967
*
70-
* <p>A cache aspect is serializable if its {@code CacheResolver} and
71-
* {@code CacheOperationSource} are serializable.
68+
* <p>Note: A cache aspect is serializable but does not perform any actual caching
69+
* after deserialization.
7270
*
7371
* @author Costin Leau
7472
* @author Juergen Hoeller
@@ -132,7 +130,7 @@ public CacheOperationSource getCacheOperationSource() {
132130
/**
133131
* Set the default {@link KeyGenerator} that this cache aspect should delegate to
134132
* if no specific key generator has been set for the operation.
135-
* <p>The default is a {@link SimpleKeyGenerator}
133+
* <p>The default is a {@link SimpleKeyGenerator}.
136134
*/
137135
public void setKeyGenerator(KeyGenerator keyGenerator) {
138136
this.keyGenerator = keyGenerator;
@@ -145,22 +143,12 @@ public KeyGenerator getKeyGenerator() {
145143
return this.keyGenerator;
146144
}
147145

148-
/**
149-
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
150-
* Replace the current {@link CacheResolver}, if any.
151-
* @see #setCacheResolver(CacheResolver)
152-
* @see SimpleCacheResolver
153-
*/
154-
public void setCacheManager(CacheManager cacheManager) {
155-
this.cacheResolver = new SimpleCacheResolver(cacheManager);
156-
}
157-
158146
/**
159147
* Set the default {@link CacheResolver} that this cache aspect should delegate
160148
* to if no specific cache resolver has been set for the operation.
161149
* <p>The default resolver resolves the caches against their names and the
162150
* default cache manager.
163-
* @see #setCacheManager(org.springframework.cache.CacheManager)
151+
* @see #setCacheManager
164152
* @see SimpleCacheResolver
165153
*/
166154
public void setCacheResolver(CacheResolver cacheResolver) {
@@ -174,6 +162,16 @@ public CacheResolver getCacheResolver() {
174162
return this.cacheResolver;
175163
}
176164

165+
/**
166+
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
167+
* Replace the current {@link CacheResolver}, if any.
168+
* @see #setCacheResolver
169+
* @see SimpleCacheResolver
170+
*/
171+
public void setCacheManager(CacheManager cacheManager) {
172+
this.cacheResolver = new SimpleCacheResolver(cacheManager);
173+
}
174+
177175
/**
178176
* Set the containing {@link BeanFactory} for {@link CacheManager} and other
179177
* service lookups.

spring-context/src/main/java/org/springframework/cache/interceptor/CacheProxyFactoryBean.java

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,10 @@
1919
import org.springframework.aop.Pointcut;
2020
import org.springframework.aop.framework.AbstractSingletonProxyFactoryBean;
2121
import org.springframework.aop.support.DefaultPointcutAdvisor;
22+
import org.springframework.beans.factory.BeanFactory;
23+
import org.springframework.beans.factory.BeanFactoryAware;
24+
import org.springframework.beans.factory.SmartInitializingSingleton;
25+
import org.springframework.cache.CacheManager;
2226

2327
/**
2428
* Proxy factory bean for simplified declarative caching handling.
@@ -41,18 +45,53 @@
4145
* @see CacheInterceptor
4246
*/
4347
@SuppressWarnings("serial")
44-
public class CacheProxyFactoryBean extends AbstractSingletonProxyFactoryBean {
48+
public class CacheProxyFactoryBean extends AbstractSingletonProxyFactoryBean
49+
implements BeanFactoryAware, SmartInitializingSingleton {
4550

46-
private final CacheInterceptor cachingInterceptor = new CacheInterceptor();
51+
private final CacheInterceptor cacheInterceptor = new CacheInterceptor();
4752

4853
private Pointcut pointcut = Pointcut.TRUE;
4954

5055

5156
/**
5257
* Set the sources used to find cache operations.
58+
* @see CacheInterceptor#setCacheOperationSources
5359
*/
5460
public void setCacheOperationSources(CacheOperationSource... cacheOperationSources) {
55-
this.cachingInterceptor.setCacheOperationSources(cacheOperationSources);
61+
this.cacheInterceptor.setCacheOperationSources(cacheOperationSources);
62+
}
63+
64+
/**
65+
* Set the default {@link KeyGenerator} that this cache aspect should delegate to
66+
* if no specific key generator has been set for the operation.
67+
* <p>The default is a {@link SimpleKeyGenerator}.
68+
* @since 5.0.3
69+
* @see CacheInterceptor#setKeyGenerator
70+
*/
71+
public void setKeyGenerator(KeyGenerator keyGenerator) {
72+
this.cacheInterceptor.setKeyGenerator(keyGenerator);
73+
}
74+
75+
/**
76+
* Set the default {@link CacheResolver} that this cache aspect should delegate
77+
* to if no specific cache resolver has been set for the operation.
78+
* <p>The default resolver resolves the caches against their names and the
79+
* default cache manager.
80+
* @since 5.0.3
81+
* @see CacheInterceptor#setCacheResolver
82+
*/
83+
public void setCacheResolver(CacheResolver cacheResolver) {
84+
this.cacheInterceptor.setCacheResolver(cacheResolver);
85+
}
86+
87+
/**
88+
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
89+
* Replace the current {@link CacheResolver}, if any.
90+
* @since 5.0.3
91+
* @see CacheInterceptor#setCacheManager
92+
*/
93+
public void setCacheManager(CacheManager cacheManager) {
94+
this.cacheInterceptor.setCacheManager(cacheManager);
5695
}
5796

5897
/**
@@ -66,10 +105,21 @@ public void setPointcut(Pointcut pointcut) {
66105
this.pointcut = pointcut;
67106
}
68107

108+
@Override
109+
public void setBeanFactory(BeanFactory beanFactory) {
110+
this.cacheInterceptor.setBeanFactory(beanFactory);
111+
}
112+
113+
@Override
114+
public void afterSingletonsInstantiated() {
115+
this.cacheInterceptor.afterSingletonsInstantiated();
116+
}
117+
118+
69119
@Override
70120
protected Object createMainInterceptor() {
71-
this.cachingInterceptor.afterPropertiesSet();
72-
return new DefaultPointcutAdvisor(this.pointcut, this.cachingInterceptor);
121+
this.cacheInterceptor.afterPropertiesSet();
122+
return new DefaultPointcutAdvisor(this.pointcut, this.cacheInterceptor);
73123
}
74124

75125
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cache.interceptor;
18+
19+
import java.util.Arrays;
20+
import java.util.concurrent.atomic.AtomicBoolean;
21+
22+
import org.junit.Test;
23+
24+
import org.springframework.cache.annotation.EnableCaching;
25+
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
26+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
import static org.junit.Assert.*;
31+
32+
/**
33+
* Integration tests for {@link CacheProxyFactoryBean}.
34+
*
35+
* @author John Blum
36+
* @author Juergen Hoeller
37+
*/
38+
public class CacheProxyFactoryBeanTests {
39+
40+
@Test
41+
public void configurationClassWithCacheProxyFactoryBean() {
42+
try (AnnotationConfigApplicationContext applicationContext =
43+
new AnnotationConfigApplicationContext(CacheProxyFactoryBeanConfiguration.class)) {
44+
Greeter greeter = applicationContext.getBean("greeter", Greeter.class);
45+
assertNotNull(greeter);
46+
assertFalse(greeter.isCacheMiss());
47+
assertEquals("Hello John!", greeter.greet("John"));
48+
assertTrue(greeter.isCacheMiss());
49+
assertEquals("Hello Jon!", greeter.greet("Jon"));
50+
assertTrue(greeter.isCacheMiss());
51+
assertEquals("Hello John!", greeter.greet("John"));
52+
assertFalse(greeter.isCacheMiss());
53+
assertEquals("Hello World!", greeter.greet());
54+
assertTrue(greeter.isCacheMiss());
55+
assertEquals("Hello World!", greeter.greet());
56+
assertFalse(greeter.isCacheMiss());
57+
}
58+
}
59+
60+
61+
@Configuration
62+
@EnableCaching
63+
static class CacheProxyFactoryBeanConfiguration {
64+
65+
@Bean
66+
ConcurrentMapCacheManager cacheManager() {
67+
return new ConcurrentMapCacheManager("Greetings");
68+
}
69+
70+
@Bean
71+
CacheProxyFactoryBean greeter() {
72+
CacheProxyFactoryBean factoryBean = new CacheProxyFactoryBean();
73+
factoryBean.setCacheOperationSources(newCacheOperationSource("greet", newCacheOperation("Greetings")));
74+
factoryBean.setTarget(new SimpleGreeter());
75+
return factoryBean;
76+
}
77+
78+
CacheOperationSource newCacheOperationSource(String methodName, CacheOperation... cacheOperations) {
79+
NameMatchCacheOperationSource cacheOperationSource = new NameMatchCacheOperationSource();
80+
cacheOperationSource.addCacheMethod(methodName, Arrays.asList(cacheOperations));
81+
return cacheOperationSource;
82+
}
83+
84+
CacheableOperation newCacheOperation(String cacheName) {
85+
CacheableOperation.Builder builder = new CacheableOperation.Builder();
86+
builder.setCacheManager("cacheManager");
87+
builder.setCacheName(cacheName);
88+
return builder.build();
89+
}
90+
}
91+
92+
93+
interface Greeter {
94+
95+
default boolean isCacheHit() {
96+
return !isCacheMiss();
97+
}
98+
99+
boolean isCacheMiss();
100+
101+
void setCacheMiss();
102+
103+
default String greet() {
104+
return greet("World");
105+
}
106+
107+
default String greet(String name) {
108+
setCacheMiss();
109+
return String.format("Hello %s!", name);
110+
}
111+
}
112+
113+
114+
static class SimpleGreeter implements Greeter {
115+
116+
private final AtomicBoolean cacheMiss = new AtomicBoolean(false);
117+
118+
@Override
119+
public boolean isCacheMiss() {
120+
return this.cacheMiss.getAndSet(false);
121+
}
122+
123+
@Override
124+
public void setCacheMiss() {
125+
this.cacheMiss.set(true);
126+
}
127+
}
128+
129+
}

0 commit comments

Comments
 (0)