[英]How to set auto location in PropertySourcesPlaceholderConfigurer spring boot?
[英]Spring boot PropertySourcesPlaceHolderConfigurer with @ProperySource
我有一個關於PropertySourcesPlaceholderConfigurer
和@PropertySource
批注的簡單問題。 我有簡單的豆類。 我想通過@Value
注釋從application.properties
和其他一些test.properties
注入屬性。 我閱讀了幾份資料,其中@PropertySource
需要定義靜態PropertySourcesPlaceholderConfigurer
bean。 從文檔:
為了使用PropertySource的屬性解析$ {...}定義或@Value注釋中的占位符,必須注冊一個PropertySourcesPlaceholderConfigurer。 在XML中使用時會自動發生,但是在使用@Configuration類時必須使用靜態@Bean方法顯式注冊。
但是對我來說,沒有這個bean就可以正常工作。 Spring是否以某種方式在應用程序啟動時自動配置PropertySourcesPlaceholderConfigurer
? 這是我的簡單示例(第一個屬性來自application.properties
,第二個屬性來自test.properties
):
@Configuration
@PropertySource("classpath:test.properties")
public class AppConfiguration {
@Value("${first.property}")
private String firstProp;
@Value("${second.property}")
private String secondProp;
@Bean
public TestModel getModel() {
TestModel model = new TestModel();
model.setFirstProperty(firstProp);
model.setSecondProperty(secondProp);
return model;
}
}
靜態PropertySourcesPlaceholderConfigurer
bean被PropertyPlaceholderAutoConfiguration
類丟失時會自動注冊,該類似乎是在Spring Boot 1.5.0.RELEASE中引入的(因為在以前的版本中沒有在線javadoc)。
有趣的是, Spring Boot 1.5發行說明中沒有提到這種新的自動配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.