|
| 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.Set; |
| 20 | + |
| 21 | +import org.springframework.beans.BeansException; |
| 22 | +import org.springframework.beans.factory.BeanFactory; |
| 23 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 24 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 25 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 26 | +import org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzerSupport; |
| 27 | +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; |
| 28 | +import org.springframework.boot.diagnostics.FailureAnalysis; |
| 29 | +import org.springframework.cache.CacheManager; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.CollectionUtils; |
| 32 | +import org.springframework.util.StringUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * An {@link AbstractFailureAnalyzer} for |
| 36 | + * {@link CacheAutoConfiguration.NoSuchCacheManagerException}. |
| 37 | + * |
| 38 | + * @author Dmytro Nosan |
| 39 | + */ |
| 40 | +class NoSuchCacheManagerFailureAnalyzer extends |
| 41 | + NoSuchBeanDefinitionFailureAnalyzerSupport<CacheAutoConfiguration.NoSuchCacheManagerException> { |
| 42 | + |
| 43 | + private ConfigurableListableBeanFactory beanFactory; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 47 | + Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory); |
| 48 | + this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; |
| 49 | + super.setBeanFactory(beanFactory); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public FailureAnalysis analyze(Throwable failure) { |
| 54 | + CacheAutoConfiguration.NoUniqueCacheManagerException cause = findCause(failure, |
| 55 | + CacheAutoConfiguration.NoUniqueCacheManagerException.class); |
| 56 | + if (cause != null) { |
| 57 | + return analyzeNoUniqueCacheManagerException(cause); |
| 58 | + } |
| 59 | + return super.analyze(failure); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected BeanMetadata getBeanMetadata(Throwable rootFailure, |
| 64 | + CacheAutoConfiguration.NoSuchCacheManagerException cause) { |
| 65 | + return new BeanMetadata(CacheManager.class); |
| 66 | + } |
| 67 | + |
| 68 | + private FailureAnalysis analyzeNoUniqueCacheManagerException( |
| 69 | + CacheAutoConfiguration.NoUniqueCacheManagerException cause) { |
| 70 | + Set<String> beanNames = cause.getBeanNames(); |
| 71 | + if (CollectionUtils.isEmpty(beanNames)) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + StringBuilder description = new StringBuilder(); |
| 75 | + description.append(String.format("Found %d beans of the type " + "'%s'%n", |
| 76 | + beanNames.size(), CacheManager.class.getName())); |
| 77 | + for (String beanName : beanNames) { |
| 78 | + try { |
| 79 | + BeanDefinition definition = this.beanFactory |
| 80 | + .getMergedBeanDefinition(beanName); |
| 81 | + if (StringUtils.hasText(definition.getFactoryMethodName())) { |
| 82 | + description.append( |
| 83 | + String.format("\t- '%s': defined by method '%s' in %s%n", |
| 84 | + beanName, definition.getFactoryMethodName(), |
| 85 | + definition.getResourceDescription())); |
| 86 | + } |
| 87 | + else { |
| 88 | + description.append(String.format("\t- '%s': defined in %s%n", |
| 89 | + beanName, definition.getResourceDescription())); |
| 90 | + } |
| 91 | + } |
| 92 | + catch (NoSuchBeanDefinitionException ex) { |
| 93 | + description.append(String.format( |
| 94 | + "\t- '%s': a programmatically registered singleton", beanName)); |
| 95 | + } |
| 96 | + } |
| 97 | + return new FailureAnalysis(description.toString(), |
| 98 | + "Consider marking one of the beans as @Primary, updating the consumer to" |
| 99 | + + " accept multiple beans, or using @Qualifier to identify the" |
| 100 | + + " bean that should be consumed", |
| 101 | + cause); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments