SpringBoot使用@PropertySource读取 properties 配置

编程入门 行业动态 更新时间:2024-10-27 20:39:35

<a href=https://www.elefans.com/category/jswz/34/1769943.html style=SpringBoot使用@PropertySource读取 properties 配置"/>

SpringBoot使用@PropertySource读取 properties 配置

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);}

参考资料:

/

更多推荐

SpringBoot使用@PropertySource读取 properties 配置

本文发布于:2023-11-15 04:33:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SpringBoot   PropertySource   properties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!