姿势解锁 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"));

具体细节请参考代码库
 


转载于:https://www.cnblogs.com/lyc88/articles/10991799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值