Skip to content

Commit e2518e0

Browse files
committed
Reduced DefaultJCacheOperationSource's dependency from ApplicationContext to BeanFactory
Issue: SPR-12336
1 parent 0e36402 commit e2518e0

File tree

3 files changed

+80
-58
lines changed

3 files changed

+80
-58
lines changed

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/DefaultJCacheOperationSource.java

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.cache.jcache.interceptor;
1818

1919
import org.springframework.beans.BeanUtils;
20+
import org.springframework.beans.factory.BeanFactory;
21+
import org.springframework.beans.factory.BeanFactoryAware;
2022
import org.springframework.beans.factory.InitializingBean;
2123
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2224
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
@@ -26,8 +28,6 @@
2628
import org.springframework.cache.interceptor.KeyGenerator;
2729
import org.springframework.cache.interceptor.SimpleCacheResolver;
2830
import org.springframework.cache.interceptor.SimpleKeyGenerator;
29-
import org.springframework.context.ApplicationContext;
30-
import org.springframework.context.ApplicationContextAware;
3131
import org.springframework.util.Assert;
3232

3333
/**
@@ -39,19 +39,19 @@
3939
* @since 4.1
4040
*/
4141
public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSource
42-
implements ApplicationContextAware, InitializingBean, SmartInitializingSingleton {
42+
implements BeanFactoryAware, InitializingBean, SmartInitializingSingleton {
4343

4444
private CacheManager cacheManager;
4545

46-
private KeyGenerator keyGenerator = new SimpleKeyGenerator();
47-
48-
private KeyGenerator adaptedKeyGenerator;
49-
5046
private CacheResolver cacheResolver;
5147

5248
private CacheResolver exceptionCacheResolver;
5349

54-
private ApplicationContext applicationContext;
50+
private KeyGenerator keyGenerator = new SimpleKeyGenerator();
51+
52+
private KeyGenerator adaptedKeyGenerator;
53+
54+
private BeanFactory beanFactory;
5555

5656

5757
/**
@@ -63,16 +63,10 @@ public void setCacheManager(CacheManager cacheManager) {
6363
}
6464

6565
/**
66-
* Set the default {@link KeyGenerator}. If none is set, a {@link SimpleKeyGenerator}
67-
* honoringKe the JSR-107 {@link javax.cache.annotation.CacheKey} and
68-
* {@link javax.cache.annotation.CacheValue} will be used.
66+
* Return the specified cache manager to use, if any.
6967
*/
70-
public void setKeyGenerator(KeyGenerator keyGenerator) {
71-
this.keyGenerator = keyGenerator;
72-
}
73-
74-
public KeyGenerator getKeyGenerator() {
75-
return this.keyGenerator;
68+
public CacheManager getCacheManager() {
69+
return this.cacheManager;
7670
}
7771

7872
/**
@@ -83,8 +77,11 @@ public void setCacheResolver(CacheResolver cacheResolver) {
8377
this.cacheResolver = cacheResolver;
8478
}
8579

80+
/**
81+
* Return the specified cache resolver to use, if any.
82+
*/
8683
public CacheResolver getCacheResolver() {
87-
return getDefaultCacheResolver();
84+
return this.cacheResolver;
8885
}
8986

9087
/**
@@ -95,13 +92,32 @@ public void setExceptionCacheResolver(CacheResolver exceptionCacheResolver) {
9592
this.exceptionCacheResolver = exceptionCacheResolver;
9693
}
9794

95+
/**
96+
* Return the specified exception cache resolver to use, if any.
97+
*/
9898
public CacheResolver getExceptionCacheResolver() {
99-
return getDefaultExceptionCacheResolver();
99+
return this.exceptionCacheResolver;
100+
}
101+
102+
/**
103+
* Set the default {@link KeyGenerator}. If none is set, a {@link SimpleKeyGenerator}
104+
* honoringKe the JSR-107 {@link javax.cache.annotation.CacheKey} and
105+
* {@link javax.cache.annotation.CacheValue} will be used.
106+
*/
107+
public void setKeyGenerator(KeyGenerator keyGenerator) {
108+
this.keyGenerator = keyGenerator;
109+
}
110+
111+
/**
112+
* Return the specified key generator to use, if any.
113+
*/
114+
public KeyGenerator getKeyGenerator() {
115+
return this.keyGenerator;
100116
}
101117

102118
@Override
103-
public void setApplicationContext(ApplicationContext applicationContext) {
104-
this.applicationContext = applicationContext;
119+
public void setBeanFactory(BeanFactory beanFactory) {
120+
this.beanFactory = beanFactory;
105121
}
106122

107123

@@ -121,7 +137,7 @@ public void afterSingletonsInstantiated() {
121137
@Override
122138
protected <T> T getBean(Class<T> type) {
123139
try {
124-
return this.applicationContext.getBean(type);
140+
return this.beanFactory.getBean(type);
125141
}
126142
catch (NoUniqueBeanDefinitionException ex) {
127143
throw new IllegalStateException("No unique [" + type.getName() + "] bean found in application context - " +
@@ -135,18 +151,35 @@ protected <T> T getBean(Class<T> type) {
135151
}
136152
}
137153

154+
protected CacheManager getDefaultCacheManager() {
155+
if (this.cacheManager == null) {
156+
try {
157+
this.cacheManager = this.beanFactory.getBean(CacheManager.class);
158+
}
159+
catch (NoUniqueBeanDefinitionException ex) {
160+
throw new IllegalStateException("No unique bean of type CacheManager found. "+
161+
"Mark one as primary or declare a specific CacheManager to use.");
162+
}
163+
catch (NoSuchBeanDefinitionException ex) {
164+
throw new IllegalStateException("No bean of type CacheManager found. Register a CacheManager "+
165+
"bean or remove the @EnableCaching annotation from your configuration.");
166+
}
167+
}
168+
return this.cacheManager;
169+
}
170+
138171
@Override
139172
protected CacheResolver getDefaultCacheResolver() {
140173
if (this.cacheResolver == null) {
141-
this.cacheResolver = new SimpleCacheResolver(getCacheManager());
174+
this.cacheResolver = new SimpleCacheResolver(getDefaultCacheManager());
142175
}
143176
return this.cacheResolver;
144177
}
145178

146179
@Override
147180
protected CacheResolver getDefaultExceptionCacheResolver() {
148181
if (this.exceptionCacheResolver == null) {
149-
this.exceptionCacheResolver = new SimpleExceptionCacheResolver(getCacheManager());
182+
this.exceptionCacheResolver = new SimpleExceptionCacheResolver(getDefaultCacheManager());
150183
}
151184
return this.exceptionCacheResolver;
152185
}
@@ -156,21 +189,4 @@ protected KeyGenerator getDefaultKeyGenerator() {
156189
return this.adaptedKeyGenerator;
157190
}
158191

159-
private CacheManager getCacheManager() {
160-
if (this.cacheManager == null) {
161-
try {
162-
this.cacheManager = this.applicationContext.getBean(CacheManager.class);
163-
}
164-
catch (NoUniqueBeanDefinitionException ex) {
165-
throw new IllegalStateException("No unique bean of type CacheManager found. "+
166-
"Mark one as primary or declare a specific CacheManager to use.");
167-
}
168-
catch (NoSuchBeanDefinitionException ex) {
169-
throw new IllegalStateException("No bean of type CacheManager found. Register a CacheManager "+
170-
"bean or remove the @EnableCaching annotation from your configuration.");
171-
}
172-
}
173-
return this.cacheManager;
174-
}
175-
176192
}

spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/AnnotationCacheOperationSourceTests.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import org.junit.Before;
2828
import org.junit.Test;
2929

30+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3031
import org.springframework.cache.interceptor.CacheResolver;
3132
import org.springframework.cache.interceptor.KeyGenerator;
3233
import org.springframework.cache.jcache.AbstractJCacheTests;
3334
import org.springframework.cache.jcache.support.TestableCacheKeyGenerator;
3435
import org.springframework.cache.jcache.support.TestableCacheResolver;
3536
import org.springframework.cache.jcache.support.TestableCacheResolverFactory;
36-
import org.springframework.context.support.StaticApplicationContext;
3737
import org.springframework.util.Assert;
3838
import org.springframework.util.ReflectionUtils;
3939

@@ -47,17 +47,19 @@ public class AnnotationCacheOperationSourceTests extends AbstractJCacheTests {
4747

4848
private final DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
4949

50-
private final StaticApplicationContext applicationContext = new StaticApplicationContext();
50+
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
51+
5152

5253
@Before
5354
public void setUp() {
54-
source.setApplicationContext(applicationContext);
55-
source.setKeyGenerator(defaultKeyGenerator);
5655
source.setCacheResolver(defaultCacheResolver);
5756
source.setExceptionCacheResolver(defaultExceptionCacheResolver);
57+
source.setKeyGenerator(defaultKeyGenerator);
58+
source.setBeanFactory(beanFactory);
5859
source.afterPropertiesSet();
5960
}
6061

62+
6163
@Test
6264
public void cache() {
6365
CacheResultOperation op = getDefaultCacheOperation(CacheResultOperation.class, String.class);
@@ -104,29 +106,29 @@ public void multiAnnotations() {
104106

105107
@Test
106108
public void defaultCacheNameWithCandidate() {
107-
Method m = ReflectionUtils.findMethod(Object.class, "toString");
108-
assertEquals("foo", source.determineCacheName(m, null, "foo"));
109+
Method method = ReflectionUtils.findMethod(Object.class, "toString");
110+
assertEquals("foo", source.determineCacheName(method, null, "foo"));
109111
}
110112

111113
@Test
112114
public void defaultCacheNameWithDefaults() {
113-
Method m = ReflectionUtils.findMethod(Object.class, "toString");
115+
Method method = ReflectionUtils.findMethod(Object.class, "toString");
114116
CacheDefaults mock = mock(CacheDefaults.class);
115117
given(mock.cacheName()).willReturn("");
116-
assertEquals("java.lang.Object.toString()", source.determineCacheName(m, mock, ""));
118+
assertEquals("java.lang.Object.toString()", source.determineCacheName(method, mock, ""));
117119
}
118120

119121
@Test
120122
public void defaultCacheNameNoDefaults() {
121-
Method m = ReflectionUtils.findMethod(Object.class, "toString");
122-
assertEquals("java.lang.Object.toString()", source.determineCacheName(m, null, ""));
123+
Method method = ReflectionUtils.findMethod(Object.class, "toString");
124+
assertEquals("java.lang.Object.toString()", source.determineCacheName(method, null, ""));
123125
}
124126

125127
@Test
126128
public void defaultCacheNameWithParameters() {
127-
Method m = ReflectionUtils.findMethod(Comparator.class, "compare", Object.class, Object.class);
129+
Method method = ReflectionUtils.findMethod(Comparator.class, "compare", Object.class, Object.class);
128130
assertEquals("java.util.Comparator.compare(java.lang.Object,java.lang.Object)",
129-
source.determineCacheName(m, null, ""));
131+
source.determineCacheName(method, null, ""));
130132
}
131133

132134
@Test
@@ -151,7 +153,7 @@ public void customKeyGenerator() {
151153
@Test
152154
public void customKeyGeneratorSpringBean() {
153155
TestableCacheKeyGenerator bean = new TestableCacheKeyGenerator();
154-
applicationContext.getBeanFactory().registerSingleton("fooBar", bean);
156+
beanFactory.registerSingleton("fooBar", bean);
155157
CacheResultOperation operation =
156158
getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class);
157159
assertEquals(defaultCacheResolver, operation.getCacheResolver());
@@ -188,8 +190,9 @@ protected <T extends JCacheOperation<?>> T getDefaultCacheOperation(Class<T> ope
188190
return getCacheOperation(operationType, AnnotatedJCacheableService.class, name.getMethodName(), parameterTypes);
189191
}
190192

191-
protected <T extends JCacheOperation<?>> T getCacheOperation(Class<T> operationType, Class<?> targetType,
192-
String methodName, Class<?>... parameterTypes) {
193+
protected <T extends JCacheOperation<?>> T getCacheOperation(
194+
Class<T> operationType, Class<?> targetType, String methodName, Class<?>... parameterTypes) {
195+
193196
JCacheOperation<?> result = getCacheOperation(targetType, methodName, parameterTypes);
194197
assertNotNull(result);
195198
assertEquals(operationType, result.getClass());
@@ -204,6 +207,7 @@ private JCacheOperation<?> getCacheOperation(Class<?> targetType, String methodN
204207

205208
private void assertJCacheResolver(CacheResolver actual,
206209
Class<? extends javax.cache.annotation.CacheResolver> expectedTargetType) {
210+
207211
if (expectedTargetType == null) {
208212
assertNull(actual);
209213
}
@@ -240,6 +244,7 @@ public Object customCacheResolver(Long id) {
240244
}
241245
}
242246

247+
243248
@CacheDefaults(cacheResolverFactory = TestableCacheResolverFactory.class,
244249
cacheKeyGenerator = TestableCacheKeyGenerator.class)
245250
static class CustomServiceWithDefaults {
@@ -255,6 +260,7 @@ public Object customKeyGeneratorAndCacheResolverWithExceptionName(Long id) {
255260
}
256261
}
257262

263+
258264
static class InvalidCases {
259265

260266
@CacheRemove

spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/JCacheInterceptorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
import org.junit.Test;
2222

23+
import org.springframework.beans.factory.support.StaticListableBeanFactory;
2324
import org.springframework.cache.CacheManager;
2425
import org.springframework.cache.interceptor.CacheOperationInvoker;
2526
import org.springframework.cache.interceptor.CacheResolver;
2627
import org.springframework.cache.interceptor.KeyGenerator;
2728
import org.springframework.cache.interceptor.NamedCacheResolver;
2829
import org.springframework.cache.jcache.AbstractJCacheTests;
29-
import org.springframework.context.support.StaticApplicationContext;
3030
import org.springframework.util.ReflectionUtils;
3131

3232
import static org.junit.Assert.*;
@@ -108,11 +108,11 @@ protected JCacheOperationSource createOperationSource(CacheManager cacheManager,
108108
CacheResolver cacheResolver, CacheResolver exceptionCacheResolver, KeyGenerator keyGenerator) {
109109

110110
DefaultJCacheOperationSource source = new DefaultJCacheOperationSource();
111-
source.setApplicationContext(new StaticApplicationContext());
112111
source.setCacheManager(cacheManager);
113112
source.setCacheResolver(cacheResolver);
114113
source.setExceptionCacheResolver(exceptionCacheResolver);
115114
source.setKeyGenerator(keyGenerator);
115+
source.setBeanFactory(new StaticListableBeanFactory());
116116
source.afterPropertiesSet();
117117
source.afterSingletonsInstantiated();
118118
return source;

0 commit comments

Comments
 (0)