17
17
package org .springframework .boot .autoconfigure .cache ;
18
18
19
19
import java .util .List ;
20
+ import java .util .Map ;
21
+ import java .util .Set ;
20
22
21
23
import org .springframework .beans .factory .InitializingBean ;
22
24
import org .springframework .beans .factory .ObjectProvider ;
43
45
import org .springframework .core .type .AnnotationMetadata ;
44
46
import org .springframework .orm .jpa .AbstractEntityManagerFactoryBean ;
45
47
import org .springframework .orm .jpa .LocalContainerEntityManagerFactoryBean ;
46
- import org .springframework .util .Assert ;
48
+ import org .springframework .util .CollectionUtils ;
49
+ import org .springframework .util .StringUtils ;
47
50
48
51
/**
49
52
* {@link EnableAutoConfiguration Auto-configuration} for the cache abstraction. Creates a
@@ -75,8 +78,9 @@ public CacheManagerCustomizers cacheManagerCustomizers(
75
78
76
79
@ Bean
77
80
public CacheManagerValidator cacheAutoConfigurationValidator (
78
- CacheProperties cacheProperties , ObjectProvider <CacheManager > cacheManager ) {
79
- return new CacheManagerValidator (cacheProperties , cacheManager );
81
+ CacheProperties cacheProperties ,
82
+ ObjectProvider <Map <String , CacheManager >> cacheManagers ) {
83
+ return new CacheManagerValidator (cacheProperties , cacheManagers );
80
84
}
81
85
82
86
@ Configuration
@@ -92,27 +96,29 @@ public CacheManagerJpaDependencyConfiguration() {
92
96
}
93
97
94
98
/**
95
- * Bean used to validate that a CacheManager exists and provide a more meaningful
96
- * exception.
99
+ * Bean used to validate that a CacheManager exists and it unique.
97
100
*/
98
101
static class CacheManagerValidator implements InitializingBean {
99
102
100
103
private final CacheProperties cacheProperties ;
101
104
102
- private final ObjectProvider < CacheManager > cacheManager ;
105
+ private final Map < String , CacheManager > cacheManagers ;
103
106
104
107
CacheManagerValidator (CacheProperties cacheProperties ,
105
- ObjectProvider <CacheManager > cacheManager ) {
108
+ ObjectProvider <Map < String , CacheManager >> cacheManagers ) {
106
109
this .cacheProperties = cacheProperties ;
107
- this .cacheManager = cacheManager ;
110
+ this .cacheManagers = cacheManagers . getIfAvailable () ;
108
111
}
109
112
110
113
@ Override
111
114
public void afterPropertiesSet () {
112
- Assert .notNull (this .cacheManager .getIfAvailable (),
113
- () -> "No cache manager could "
114
- + "be auto-configured, check your configuration (caching "
115
- + "type is '" + this .cacheProperties .getType () + "')" );
115
+ if (CollectionUtils .isEmpty (this .cacheManagers )) {
116
+ throw new NoSuchCacheManagerException (this .cacheProperties );
117
+ }
118
+ if (this .cacheManagers .size () > 1 ) {
119
+ throw new NoUniqueCacheManagerException (this .cacheManagers .keySet (),
120
+ this .cacheProperties );
121
+ }
116
122
}
117
123
118
124
}
@@ -134,4 +140,52 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
134
140
135
141
}
136
142
143
+ /**
144
+ * Exception thrown when {@link org.springframework.cache.CacheManager} implementation
145
+ * are not specified.
146
+ */
147
+ static class NoSuchCacheManagerException extends RuntimeException {
148
+
149
+ private final CacheProperties properties ;
150
+
151
+ NoSuchCacheManagerException (CacheProperties properties ) {
152
+ super (String .format ("No qualifying bean of type '%s' available" ,
153
+ CacheManager .class .getName ()));
154
+ this .properties = properties ;
155
+ }
156
+
157
+ NoSuchCacheManagerException (String message , CacheProperties properties ) {
158
+ super (message );
159
+ this .properties = properties ;
160
+ }
161
+
162
+ CacheProperties getProperties () {
163
+ return this .properties ;
164
+ }
165
+
166
+ }
167
+
168
+ /**
169
+ * Exception thrown when multiple {@link org.springframework.cache.CacheManager}
170
+ * implementations are available with no way to know which implementation should be
171
+ * used.
172
+ */
173
+ static class NoUniqueCacheManagerException extends NoSuchCacheManagerException {
174
+
175
+ private final Set <String > beanNames ;
176
+
177
+ NoUniqueCacheManagerException (Set <String > beanNames , CacheProperties properties ) {
178
+ super (String .format (
179
+ "expected single matching bean of type '%s' but found " + "%d: %s" ,
180
+ CacheManager .class .getName (), beanNames .size (),
181
+ StringUtils .collectionToCommaDelimitedString (beanNames )), properties );
182
+ this .beanNames = beanNames ;
183
+ }
184
+
185
+ Set <String > getBeanNames () {
186
+ return this .beanNames ;
187
+ }
188
+
189
+ }
190
+
137
191
}
0 commit comments