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