SpringBootプロファイルの読み込み(classpath/fileからyml/propertiesファイルを読み込む)

1443 ワード

一、propertiesファイルの読み込み
コンフィギュレーションアイテム@PropertySourceの使用
 
二、ymlファイルの読み取り
起動クラスに次のコードを追加します.
 
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
    yaml.setResources(new FileSystemResource("/dir/application.yml"));//File  
    //yaml.setResources(new ClassPathResource("application.yml"));//class  
    configurer.setProperties(yaml.getObject());
    return configurer;
}
 

 
1、複数のプロファイルを導入する:
yaml.setResources(new ClassPathResource("application1.yml"), new ClassPathResource("application2.yml"));