第一次书写简书。。。不知道排版和页面会不会好看。唉,凑活来吧。开干。
本文会分享三种读取配置文件的方式:
1.使用Environment 对象接口,直接就像使用别的接口一样,@Autowired 注入交给spring管理即可。
2.使用@Value("${xxx}")的方式可以快速使用。
3.使用ConfigurationProperties注解,可以将配置文件的属性映射到对应的对应的configBean类上。(不过一般好像是用在yml的方式比较合适)
一、使用Environment 对象接口读取properties属性。
不多说,直接上代码。

配置文件中设置测试属性:
app.title=Learning Spring Boot
app.description=Working with the properties file
测试结果:

二、使用@Value读取properties属性。

测试结果:

网友评论