SpringBoot讀取配置文件(從classpath/file讀取yml/properties文件)


一、讀取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"));
 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM