spring boot 自定义配置文件外置(配置文件放在jar同级目录或同级目录config文件夹下)

参考:

https://www.jb51.net/article/141981.htm

spring boot默认的配置文件application.properties可以直接外置,jar在运行时自动读取,默认优先级:

config/application.properties  >  application.properties > classpath:application.properties

如果是自定义配置文件,例如下面的config.properties,想要spring boot的jar在运行时读取外置的config.properties文件,可以使用如下方式:

config.filePath=E:/materialFile/
config.serverPath=http://192.18.0.0:8080
@Data  //lombok注解,自动生成setter和getter
@Component //bean被spring管理
@ConfigurationProperties(prefix = "config") //配置文件key的前缀
@PropertySource(value = {"classpath:config.properties","file:config/config.properties"},ignoreResourceNotFound =true)
public class ConfigProperties {
    private String serverPath;
    private String filePath;
}

最重要的是@PropertySource注解的配置,上面的ConfigProperties 属性源有两个:
“classpath:config.properties"和"file:config/config.properties”

这两个属性分别表示类路径下的配置文件和jar包路径下的config文件夹下的配置文件。这里将"file:config/config.properties"放在后面,代表优先使用jar包路径下config文件夹下的配置文件,如果没有,就使用类路径下的。在这样使用时,需要添加ignoreResourceNotFound =true 配置,避免第二个路径找不到的情况下spring boot启动时报错(找不到文件)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值