簡體   English   中英

春季啟動PropertySourcesPlaceHolderConfigurer與@ProperySource

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM