17
17
package org .springframework .cache .jcache .interceptor ;
18
18
19
19
import org .springframework .beans .BeanUtils ;
20
+ import org .springframework .beans .factory .BeanFactory ;
21
+ import org .springframework .beans .factory .BeanFactoryAware ;
20
22
import org .springframework .beans .factory .InitializingBean ;
21
23
import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
22
24
import org .springframework .beans .factory .NoUniqueBeanDefinitionException ;
26
28
import org .springframework .cache .interceptor .KeyGenerator ;
27
29
import org .springframework .cache .interceptor .SimpleCacheResolver ;
28
30
import org .springframework .cache .interceptor .SimpleKeyGenerator ;
29
- import org .springframework .context .ApplicationContext ;
30
- import org .springframework .context .ApplicationContextAware ;
31
31
import org .springframework .util .Assert ;
32
32
33
33
/**
39
39
* @since 4.1
40
40
*/
41
41
public class DefaultJCacheOperationSource extends AnnotationJCacheOperationSource
42
- implements ApplicationContextAware , InitializingBean , SmartInitializingSingleton {
42
+ implements BeanFactoryAware , InitializingBean , SmartInitializingSingleton {
43
43
44
44
private CacheManager cacheManager ;
45
45
46
- private KeyGenerator keyGenerator = new SimpleKeyGenerator ();
47
-
48
- private KeyGenerator adaptedKeyGenerator ;
49
-
50
46
private CacheResolver cacheResolver ;
51
47
52
48
private CacheResolver exceptionCacheResolver ;
53
49
54
- private ApplicationContext applicationContext ;
50
+ private KeyGenerator keyGenerator = new SimpleKeyGenerator ();
51
+
52
+ private KeyGenerator adaptedKeyGenerator ;
53
+
54
+ private BeanFactory beanFactory ;
55
55
56
56
57
57
/**
@@ -63,16 +63,10 @@ public void setCacheManager(CacheManager cacheManager) {
63
63
}
64
64
65
65
/**
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.
69
67
*/
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 ;
76
70
}
77
71
78
72
/**
@@ -83,8 +77,11 @@ public void setCacheResolver(CacheResolver cacheResolver) {
83
77
this .cacheResolver = cacheResolver ;
84
78
}
85
79
80
+ /**
81
+ * Return the specified cache resolver to use, if any.
82
+ */
86
83
public CacheResolver getCacheResolver () {
87
- return getDefaultCacheResolver () ;
84
+ return this . cacheResolver ;
88
85
}
89
86
90
87
/**
@@ -95,13 +92,32 @@ public void setExceptionCacheResolver(CacheResolver exceptionCacheResolver) {
95
92
this .exceptionCacheResolver = exceptionCacheResolver ;
96
93
}
97
94
95
+ /**
96
+ * Return the specified exception cache resolver to use, if any.
97
+ */
98
98
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 ;
100
116
}
101
117
102
118
@ Override
103
- public void setApplicationContext ( ApplicationContext applicationContext ) {
104
- this .applicationContext = applicationContext ;
119
+ public void setBeanFactory ( BeanFactory beanFactory ) {
120
+ this .beanFactory = beanFactory ;
105
121
}
106
122
107
123
@@ -121,7 +137,7 @@ public void afterSingletonsInstantiated() {
121
137
@ Override
122
138
protected <T > T getBean (Class <T > type ) {
123
139
try {
124
- return this .applicationContext .getBean (type );
140
+ return this .beanFactory .getBean (type );
125
141
}
126
142
catch (NoUniqueBeanDefinitionException ex ) {
127
143
throw new IllegalStateException ("No unique [" + type .getName () + "] bean found in application context - " +
@@ -135,18 +151,35 @@ protected <T> T getBean(Class<T> type) {
135
151
}
136
152
}
137
153
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
+
138
171
@ Override
139
172
protected CacheResolver getDefaultCacheResolver () {
140
173
if (this .cacheResolver == null ) {
141
- this .cacheResolver = new SimpleCacheResolver (getCacheManager ());
174
+ this .cacheResolver = new SimpleCacheResolver (getDefaultCacheManager ());
142
175
}
143
176
return this .cacheResolver ;
144
177
}
145
178
146
179
@ Override
147
180
protected CacheResolver getDefaultExceptionCacheResolver () {
148
181
if (this .exceptionCacheResolver == null ) {
149
- this .exceptionCacheResolver = new SimpleExceptionCacheResolver (getCacheManager ());
182
+ this .exceptionCacheResolver = new SimpleExceptionCacheResolver (getDefaultCacheManager ());
150
183
}
151
184
return this .exceptionCacheResolver ;
152
185
}
@@ -156,21 +189,4 @@ protected KeyGenerator getDefaultKeyGenerator() {
156
189
return this .adaptedKeyGenerator ;
157
190
}
158
191
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
-
176
192
}
0 commit comments