@PropertySource与@ConfigurationProperties多种方式读取配置文件详解,附带@PropertySources使用说明
两个注解都可以读取properties文件或者xml文件,二者可以单独使用也可以结合使用。
一、 @PropertySource结合@Value读取指定配置文件
1、准备一个properties文件放在项目下
demo.name=zhangsan demo.age=25 demo.address=hangzhou2、使用@PropertySource读取数据
import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;@Data @Component @PropertySource("classpath:demo.properties") public class ReadConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;}3、查看效果
让读取到的数据在springboot启动时控制台显示
4、验证yml文件能否读取
准备一个demo.yml文件
将@PropertySource(“classpath:demo.yml”)改为,运行报错。源码给了value的说明,指明@PropertySource仅支持读取properties文件与xml文件。
二、 @PropertySource结合Environment读取配置文件
import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component;@Component @Log4j2 @PropertySource("classpath:demo.properties") public class ReadConfByEnvironment {@Autowiredpublic Environment environment;@Beanpublic void toReadEnvConf(){log.error("**********通过Env获取数据*********");log.error(environment.getProperty("demo.name"));log.error(environment.getProperty("demo.age"));log.error(environment.getProperty("demo.address"));log.error("**********End*********");} }三、 @PropertySource读取多个properties文件
@Data @Component @PropertySource(value = {"classpath:demo.properties","classpath:demoadd.properties"}) public class Read2ConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;@Value("${demo1.birth}")private String birth;@Value("${demo1.phone}")private String phone;@Value("${demo1.hobby}")private String hobby; }修改对应的InitRunner并运行项目
四、@PropertySource 结合 @ConfigurationProperties读取配置文件
1、准备另一个properties文件demoadd.properties:
demo1.birth=2020.9.2 demo1.phone=13020122365 demo1.hobby=codingdemo2.name=lisi demo2.age=120 demo2.address=beijing2、@PropertySource(“classpath:demoadd.properties”)作用是指定文件,@ConfigurationProperties(prefix=“demo1”)限定前缀为demo1,前缀为demo2的字段读取不到。两者结合使用时相当于读取到@PropertySource指定的文件后,再通过@ConfigurationProperties进行数据过滤,仅获取指定前缀的数据。
@Data @Component @PropertySource("classpath:demoadd.properties") @ConfigurationProperties(prefix="demo1") public class ReadConfByConfigurationProperties {private String birth;private String phone;private String hobby;}3、修改InitRunner,启动项目
@Component @Log4j2 public class InitRunner implements CommandLineRunner {@Autowiredprivate ReadConfByValue conf;@Autowiredprivate ReadConfByConfigurationProperties readconf;@Overridepublic void run(String... args) throws Exception {log.info(conf.toString());log.info(readconf.toString());} }4、如何读取多前缀数据?
从源码中看出,prefix并不是数组形式,注释中也说明了A valid prefix,表明不支持多前缀。
五、 @ConfigurationProperties单独使用
1、不通过@PropertySource指定固定文件,仅指定前缀,仍能读取到demoadd.properties中的数据
@Data @Component //@PropertySource("classpath:demoadd.properties") @ConfigurationProperties(prefix="demo1") public class ReadConfByConfigurationProperties {private String birth;private String phone;private String hobby; }2、那如果指定的prefix重复呢?
修改demo.properties文件,添加demo1属性,执行项目
本以为会报错,,谁知结果没变,还是正常输出
这不会是所有properties文件都读一遍,保留最后/或者第一次读取的文件中数据吧,
把demo.properties名字改了个遍结果也还是一动不动。。。(此处疑问保留)
3、@ConfigurationProperties能不能读取yml文件
试了试读取demo.yml,读取失败,字段全为null
如果是application.yml呢,在application.yml中添加:
执行项目
看到结果呵呵。。能读到,并且其他文件demo.properties、demoadd.properties里的demo前缀字段数据全给替换了
六、@PropertySources
实现多个配置文件的读取,可是@PropertySource本来就有这个功能啊,为什么还要搞个这?
源码:
使用方法:
@Data @Component @PropertySources(value = {@PropertySource("classpath:demo.properties"),@PropertySource("classpath:demoadd.properties")}) public class ReadConfByValue {@Value("${demo.name}")private String name;@Value("${demo.age}")private String age;@Value("${demo.address}")private String address;}总结
以上是生活随笔为你收集整理的@PropertySource与@ConfigurationProperties多种方式读取配置文件详解,附带@PropertySources使用说明的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: SQLyog连接虚拟机中mysql8.0
- 下一篇: @ConfigurationProper