已解决
SpringBoot使用@PropertySource读取 properties 配置
来自网友在路上 181881提问 提问时间:2023-11-12 22:38:55阅读次数: 81
最佳答案 问答题库818位专家为你答疑解惑
SpringBoot使用@PropertySource读取 properties 配置
properties配置文件
在resources文件夹下,新建一个文件 property-demo.properties,
示例如下:
my.config.test.name=wumy.config.test.id=123
配置的类
@PropertySource 指定配置文件。
classpath: 表示会到 target下面的class路径中查找找文件。
@Data 是 lombok 依赖包的注解,主要是用来表示 getter、 setter。
@ConfigurationProperties的 prefix 指定配置的前缀 my.config.test,比如 my.config.test.name, 就对应此类的 name属性。
/*** ConfigurationProperties的 prefix 指定配置的前缀 my.config.test,* properties文件配置的 my.config.test.name,就对应此类的 name属性。**/
@ConfigurationProperties(prefix = "my.config.test")
@PropertySource(value = "classpath:property-demo.properties",encoding = "UTF-8")
@Data
@Component
public class MyPropertySourceConfig {private String name;private Integer id;}
测试代码:
@Resourceprivate MyPropertySourceConfig myPropertySourceConfig;@Testpublic void getProperty() {String name = myPropertySourceConfig.getName();System.out.println("name: " + name);Assert.assertNotNull(name);}
参考资料:
https://blog.csdn.net/lzb348110175/article/details/105147070/
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"SpringBoot使用@PropertySource读取 properties 配置":http://eshow365.cn/6-38468-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!