springboot-加载自定义的properties文件

本文介绍了如何在SpringBoot中加载自定义的properties配置文件,如kafka.properties和amq.properties。通过在资源目录创建config子目录,放置配置文件,并在配置类上使用注解进行属性注入。文章详细讲解了两种方法:过时方法,包括在ConfigProperties类上添加@PropertySource注解和在主类启用配置支持;以及替代方法,即不使用@EnableConfigurationProperties注解,改为手动配置。经过测试,这两种方法都能使配置生效。
摘要由CSDN通过智能技术生成

在我们的开发中,有很多配置文件是需要分开配置的,例如kafka.properties,amq.properties等,那这些自定义的配置文件,怎么加载到对应的类里面了,下面就来说说这个问题。

在src/main/resources目录下新建一个config文件夹,用来存放我们的properties文件。目录结构如下:

user.properties配置文件内容如下:

com.chhliu.springboot.author=xyy
com.chhliu.springboot.age=${com.chhliu.age}
com.chhliu.springboot.sex=woman
com.chhliu.springboot.time=20170123
com.chhliu.age=27

对应的配置类如下:

public class ConfigProperties {
	
	private String author;
	private int age;
	private String sex;
	private String time;
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int ag
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值