spring boot打成jar包用外部配置文件替换配置

由于需求的原因,配置文件中的数据库连接等内容需要动态替换。

首先搜到的是可以运行jar包的时候传配置参数:

java -jar demo.jar --server.port = 9000

但是客户端传给我的是一整个大json串,所以pass。

然后了解到SpringApplication从4个地方加载配置文件:

  1. jar包同目录下的config文件夹中
  2. jar包同目录下
  3. classpath下的config文件夹中
  4. classpath目录下

优先级依次降低,前两个是从外部读取配置文件的。但由于某些原因这种方法可能也不满足要求。。

第三种:代码中指定

public class Application {
    
	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		InputStream in = new FileInputStream("application.properties");
		properties.load(in);
		SpringApplication app = new SpringApplication(Application.class);
		app.setDefaultProperties(properties);
		app.run(args);
	}
}

注意这种方式pom.xml打包时需要去除配置文件,在<build>中加:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>*.properties</exclude>
            <exclude>*.yml</exclude>
        </excludes>
    </resource>
</resources>

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值