1717package org .springframework .cache .jcache .interceptor ;
1818
1919import org .springframework .beans .BeanUtils ;
20+ import org .springframework .beans .factory .BeanFactory ;
21+ import org .springframework .beans .factory .BeanFactoryAware ;
2022import org .springframework .beans .factory .InitializingBean ;
2123import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
2224import org .springframework .beans .factory .NoUniqueBeanDefinitionException ;
2628import org .springframework .cache .interceptor .KeyGenerator ;
2729import org .springframework .cache .interceptor .SimpleCacheResolver ;
2830import org .springframework .cache .interceptor .SimpleKeyGenerator ;
29- import org .springframework .context .ApplicationContext ;
30- import org .springframework .context .ApplicationContextAware ;
3131import org .springframework .util .Assert ;
3232
3333/**
3939 * @since 4.1
4040 */
4141public 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}
0 commit comments