我正在使用Spring Boot 2.2.7.RELEASE
,并且一切正常。
当我升级到Spring Boot 2.3.0.RELEASE
时,出现了一些问题,除此之外,我能够解决它们。
我上了这个课:
@Configuration
@EnableCaching
public class CacheConfiguration extends CachingConfigurerSupport {
public static final String PROPERTY_RESOLVING_CACHE_RESOLVER_BEAN_NAME = "propertyResolvingCacheResolver";
@Value("${my.cache.name}")
private String myCacheName;
@Autowired
private Environment environment;
@Bean(PROPERTY_RESOLVING_CACHE_RESOLVER_BEAN_NAME)
@Override
public CacheResolver cacheResolver() {
return new PropertyResolvingCacheResolver(cacheManager(), environment);
}
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache(myCacheName)));
return cacheManager;
}
}
我得到的错误是:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'environment' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:814)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1282)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$ShortcutDependencyDescriptor.resolveShortcut(AutowiredAnnotationBeanPostProcessor.java:796)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1238)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.resolvedCachedArgument(AutowiredAnnotationBeanPostProcessor.java:601)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.access$000(AutowiredAnnotationBeanPostProcessor.java:131)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:631)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
... 66 common frames omitted
我认为这是咖啡豆的'生命周期'的问题,而且我的自动接线时间太早,所以我这样做了:
public class CacheConfiguration extends CachingConfigurerSupport implements EnvironmentAware {
...
private Environment environment;
...
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
...
但是随后,其他问题开始出现(执行上述操作之后),例如:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:814)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1282)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
at org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor.postProcessBeforeInitialization(ConfigurationClassPostProcessor.java:456)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
... 45 common frames omitted
这已经脱离了我的联盟,任何帮助将不胜感激,谢谢!
编辑1:
经过一番试验后,问题是因为此bean @Bean(PROPERTY_RESOLVING_CACHE_RESOLVER_BEAN_NAME)
的创建被推迟到以后的时间。
它曾经在cacheManager
之后创建,但是在2.3.0.RELEASE
中不再存在。
编辑2:
对于DataSourceHealthIndicator
和dbHealthContributor
,2.2.7.RELEASE
在应用程序上下文中都具有bean名称2.3.0.RELEASE
。
这可以在/ actuator / beans下看到:
"dbHealthContributor": {
"aliases": [],
"scope": "singleton",
"type": "org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator",
"resource": "class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthContributorAutoConfiguration.class]",
"dependencies": [
"dataSource"
]
},
答案 0 :(得分:1)
游戏有点晚了,但是我在将 Spring Boot 从 2.2.0 升级到 2.3.0 时遇到了同样的问题。您还可以使用 Spring 的 @Lazy
注释来解决该问题。这样 DataSourceHealthIndicator
将在稍后实际使用时自动装配,到那时自动配置将使其可用于自动装配。而且您不必像在答案中那样定义自己的 bean。
所以代替
@Autowired
private DataSourceHealthIndicator dataSourceHealthIndicator;
,你会使用
@Autowired
@Lazy
private DataSourceHealthIndicator dataSourceHealthIndicator;
答案 1 :(得分:0)
通过在trace=true
中拥有application.properties
,我设法找出了问题所在。
我曾经有过这项工作(Spring Boot
免费提供 ):
@Autowired
private DataSourceHealthIndicator dataSourceHealthIndicator;
但是似乎dataSourceHealthIndicator
的创建已被延迟 ,因此在创建需要它的bean时,找不到它并且自动装配失败。 br />
因此,我手动创建了它们 :
@Bean
@Primary
public DataSourceHealthIndicator dataSourceHealthIndicator(DataSource dataSource) {
return new DataSourceHealthIndicator(dataSource, "SELECT 1 FROM DUAL");
}
我的错误消失了。
我希望能够了解发生了什么事
dataSourceHealthIndicator
和2.2.7.RELEASE
之间的2.3.0.RELEASE
{{1}}。我没有看到关于它的任何提及 Spring-Boot-2.3-Release-Notes 或我错过了它,但仍然可以看到它是“被创建的”-请参阅我的问题中的编辑2 。