[英]@ConfigurationProperites not binding to property source after upgrading to Spring Cloud Hoxton.SR7
我有一個@ConfigurationProperties
class,它不再綁定到升級到 Hoxton.SR7 后通過 Spring 雲配置解決的 YML 屬性源。 此代碼使用 Hoxton.SR4 和最新的 Spring Boot 2.2.9.RELEASE 可以正常工作。 現在,我的屬性未綁定,當我嘗試引用它們時,我收到了 NPE。 以下是我的代碼的快照:
@Configuration
public class MyConfiguration {
@Bean
public MyPropertiesBean myPropertiesBean() {
return new MyPropertiesBean();
}
}
@ConfigurationProperties(prefix = "com.acme.properties")
@Validated
public class MyPropertiesBean {
...
}
在src/main/resources/META-INF/spring.factories
:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.acme.MyConfiguration
任何想法為什么我的@ConfigurationProperties
class 在將 Spring Cloud 升級到 Hoxton.SR7 后沒有綁定?
您正在混合兩種綁定屬性的方式:class 和方法。
使用方法和@Bean
注解:
@Configuration
public class MyConfiguration {
@Bean
@ConfigurationProperties(prefix = "com.acme.properties")
@Validated
public MyPropertiesBean myPropertiesBean() {
return new MyPropertiesBean();
}
}
這將創建MyPropertiesBean
並將其存儲在應用程序上下文中以供您注入。
Class 級別的 bean 聲明還為您創建了一個 bean:
@Configuration
@ConfigurationProperties(prefix = "com.acme.properties")
@Validated
public class MyPropertiesBean {
...
}
這也將存儲一個 bean。
雖然,當您嘗試注入MyPropertiesBean
時應該會遇到運行時錯誤,因為在您的情況下,現在有兩個相同類型的 bean,並且 Spring 無法僅使用該類型解析。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.