姿势解锁 springboot 各种读取配置文件 ,从此再也不用担心不会读配置了

总结:读取配置文件直接读取也有 从git 如springconfig ,容器 Apollo 等,这里讲下直接读取的几种姿势。

    

  1. 对象直接注入  

@ConfigurationProperties(prefix = "hello")
@Autowired
private Hello hello;

2.其他文件读取 这里包含list 和 map 注入
@PropertySource(value = "classpath:hello.properties")
@ConfigurationProperties(prefix = "hello1")
@Autowired
private Hello1 hello1;

3、属性注入
@Value("${hello.name}")
private String name;

4. 环境读取
@Autowired
private Environment environment;
System.out.println(environment.getProperty("hello.name"));

5.spring 提供工具类 读取
Properties properties = PropertiesLoaderUtils.loadAllProperties("hello.properties");
System.out.println(properties.getProperty("hello1.name"));

6.自己写一个 获取
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("hello.name"));

7.Resource 加载流

 

 



具体细节请参考代码库
 


posted @ 2019-06-08 21:35  川流不息&  阅读(508)  评论(0编辑  收藏  举报