spring中@PropertySource詳細

1645 ワード

公式の説明:
https://docs.spring.io/spring/docs/5.1.7.RELEASE/spring-framework-reference/core.html#beans-usingn-propertysource
説明
@PropertySourceコメントは、SpringのEvironmentにPropertySourceを追加する便宜と声明を提供します.ap.propertiesというキーペアを含むファイルtestbean.name=myTestBeanを与えられました.以下@Configration類は以下@ProptySource方式でtestBen.get Nameを呼び出します.mysteBeatに戻ります.

@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {

    @Autowired
    Environment env;

    @Bean
    public TestBean testBean() {
        TestBean testBean = new TestBean();
        testBean.setName(env.getProperty("testbean.name"));
        return testBean;
    }
}

${…​} @PropertySource , :


@Configuration
@PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
public class AppConfig {

    @Autowired
    Environment env;

    @Bean
    public TestBean testBean() {
        TestBean testBean = new TestBean();
        testBean.setName(env.getProperty("testbean.name"));
        return testBean;
    }
}

my.placeholder ( , ), 。 , default/path 。 , IllegalArgumentException a。

@PropertySource , Java 8 。 , @PropertySource , , 。 , 。