Skip to content

Commit b160f93

Browse files
committed
CacheProxyFactoryBean exposes all relevant CacheInterceptor callbacks
Issue: SPR-16295
1 parent 44fd2d9 commit b160f93

File tree

3 files changed

+207
-31
lines changed

3 files changed

+207
-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.
@@ -51,22 +51,20 @@
5151
import org.springframework.util.StringUtils;
5252

5353
/**
54-
* Base class for caching aspects, such as the {@link CacheInterceptor}
55-
* or an AspectJ aspect.
54+
* Base class for caching aspects, such as the {@link CacheInterceptor} or an
55+
* AspectJ aspect.
5656
*
57-
* <p>This enables the underlying Spring caching infrastructure to be
58-
* used easily to implement an aspect for any aspect system.
57+
* <p>This enables the underlying Spring caching infrastructure to be used easily
58+
* to implement an aspect for any aspect system.
5959
*
60-
* <p>Subclasses are responsible for calling methods in this class in
61-
* the correct order.
60+
* <p>Subclasses are responsible for calling relevant methods in the correct order.
6261
*
63-
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheResolver}
64-
* implementation will resolve the actual cache(s) to use, and a
65-
* {@link CacheOperationSource} is used for determining caching
66-
* operations.
62+
* <p>Uses the <b>Strategy</b> design pattern. A {@link CacheOperationSource} is
63+
* used for determining caching operations, a {@link KeyGenerator} will build the
64+
* cache keys, and a {@link CacheResolver} will resolve the actual cache(s) to use.
6765
*
68-
* <p>A cache aspect is serializable if its {@code CacheResolver} and
69-
* {@code CacheOperationSource} are serializable.
66+
* <p>Note: A cache aspect is serializable but does not perform any actual caching
67+
* after deserialization.
7068
*
7169
* @author Costin Leau
7270
* @author Juergen Hoeller
@@ -121,7 +119,7 @@ public CacheOperationSource getCacheOperationSource() {
121119
/**
122120
* Set the default {@link KeyGenerator} that this cache aspect should delegate to
123121
* if no specific key generator has been set for the operation.
124-
* <p>The default is a {@link SimpleKeyGenerator}
122+
* <p>The default is a {@link SimpleKeyGenerator}.
125123
*/
126124
public void setKeyGenerator(KeyGenerator keyGenerator) {
127125
this.keyGenerator = keyGenerator;
@@ -134,22 +132,12 @@ public KeyGenerator getKeyGenerator() {
134132
return this.keyGenerator;
135133
}
136134

137-
/**
138-
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
139-
* Replace the current {@link CacheResolver}, if any.
140-
* @see #setCacheResolver(CacheResolver)
141-
* @see SimpleCacheResolver
142-
*/
143-
public void setCacheManager(CacheManager cacheManager) {
144-
this.cacheResolver = new SimpleCacheResolver(cacheManager);
145-
}
146-
147135
/**
148136
* Set the default {@link CacheResolver} that this cache aspect should delegate
149137
* to if no specific cache resolver has been set for the operation.
150138
* <p>The default resolver resolves the caches against their names and the
151139
* default cache manager.
152-
* @see #setCacheManager(org.springframework.cache.CacheManager)
140+
* @see #setCacheManager
153141
* @see SimpleCacheResolver
154142
*/
155143
public void setCacheResolver(@Nullable CacheResolver cacheResolver) {
@@ -164,6 +152,16 @@ public CacheResolver getCacheResolver() {
164152
return this.cacheResolver;
165153
}
166154

155+
/**
156+
* Set the {@link CacheManager} to use to create a default {@link CacheResolver}.
157+
* Replace the current {@link CacheResolver}, if any.
158+
* @see #setCacheResolver
159+
* @see SimpleCacheResolver
160+
*/
161+
public void setCacheManager(CacheManager cacheManager) {
162+
this.cacheResolver = new SimpleCacheResolver(cacheManager);
163+
}
164+
167165
/**
168166
* Set the containing {@link BeanFactory} for {@link CacheManager} and other
169167
* service lookups.

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

Lines changed: 55 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,11 +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+
69118

70119
@Override
71120
protected Object createMainInterceptor() {
72-
this.cachingInterceptor.afterPropertiesSet();
73-
return new DefaultPointcutAdvisor(this.pointcut, this.cachingInterceptor);
121+
this.cacheInterceptor.afterPropertiesSet();
122+
return new DefaultPointcutAdvisor(this.pointcut, this.cacheInterceptor);
74123
}
75124

76125
}
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)