Skip to content

Commit 742ba6e

Browse files
committed
- NoSuchCacheManagerFailureAnalyzer - analyzer for 'CacheManager' is not found/not unique.
- AbstractNoSuchBeanDefinitionFailureAnalyzer - provides a basic implementation to inspect `not matches` auto-configurations and registered beans. - AbstractNoUniqueBeanDefinitionFailureAnalyzer - provides a basic implementation to inspected registered beans.
1 parent df6e217 commit 742ba6e

File tree

14 files changed

+1434
-431
lines changed

14 files changed

+1434
-431
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.autoconfigure.cache;
1818

1919
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Set;
2022

2123
import org.springframework.beans.factory.InitializingBean;
2224
import org.springframework.beans.factory.ObjectProvider;
@@ -43,7 +45,8 @@
4345
import org.springframework.core.type.AnnotationMetadata;
4446
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
4547
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
46-
import org.springframework.util.Assert;
48+
import org.springframework.util.CollectionUtils;
49+
import org.springframework.util.StringUtils;
4750

4851
/**
4952
* {@link EnableAutoConfiguration Auto-configuration} for the cache abstraction. Creates a
@@ -75,8 +78,9 @@ public CacheManagerCustomizers cacheManagerCustomizers(
7578

7679
@Bean
7780
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);
8084
}
8185

8286
@Configuration
@@ -92,27 +96,29 @@ public CacheManagerJpaDependencyConfiguration() {
9296
}
9397

9498
/**
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.
97100
*/
98101
static class CacheManagerValidator implements InitializingBean {
99102

100103
private final CacheProperties cacheProperties;
101104

102-
private final ObjectProvider<CacheManager> cacheManager;
105+
private final Map<String, CacheManager> cacheManagers;
103106

104107
CacheManagerValidator(CacheProperties cacheProperties,
105-
ObjectProvider<CacheManager> cacheManager) {
108+
ObjectProvider<Map<String, CacheManager>> cacheManagers) {
106109
this.cacheProperties = cacheProperties;
107-
this.cacheManager = cacheManager;
110+
this.cacheManagers = cacheManagers.getIfAvailable();
108111
}
109112

110113
@Override
111114
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+
}
116122
}
117123

118124
}
@@ -134,4 +140,52 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
134140

135141
}
136142

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+
137191
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.cache;
18+
19+
import java.util.Collection;
20+
21+
import org.springframework.beans.BeansException;
22+
import org.springframework.beans.factory.BeanFactory;
23+
import org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzerSupport;
24+
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
25+
import org.springframework.boot.diagnostics.FailureAnalysis;
26+
import org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzerSupport;
27+
import org.springframework.cache.CacheManager;
28+
29+
/**
30+
* An {@link AbstractFailureAnalyzer} for
31+
* {@link CacheAutoConfiguration.NoSuchCacheManagerException}.
32+
*
33+
* @author Dmytro Nosan
34+
*/
35+
class NoSuchCacheManagerFailureAnalyzer extends
36+
NoSuchBeanDefinitionFailureAnalyzerSupport<CacheAutoConfiguration.NoSuchCacheManagerException> {
37+
38+
private final NoUniqueCacheManagerFailureAnalyzer analyzer = new NoUniqueCacheManagerFailureAnalyzer();
39+
40+
@Override
41+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
42+
super.setBeanFactory(beanFactory);
43+
this.analyzer.setBeanFactory(beanFactory);
44+
}
45+
46+
@Override
47+
public FailureAnalysis analyze(Throwable failure) {
48+
FailureAnalysis analyze = this.analyzer.analyze(failure);
49+
if (analyze != null) {
50+
return analyze;
51+
}
52+
return super.analyze(failure);
53+
}
54+
55+
@Override
56+
protected BeanMetadata getBeanMetadata(Throwable rootFailure,
57+
CacheAutoConfiguration.NoSuchCacheManagerException cause) {
58+
return new BeanMetadata(CacheManager.class);
59+
}
60+
61+
private static final class NoUniqueCacheManagerFailureAnalyzer extends
62+
NoUniqueBeanDefinitionFailureAnalyzerSupport<CacheAutoConfiguration.NoUniqueCacheManagerException> {
63+
64+
@Override
65+
protected Collection<String> getBeanNames(Throwable rootFailure,
66+
CacheAutoConfiguration.NoUniqueCacheManagerException cause) {
67+
return cause.getBeanNames();
68+
}
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)