设置springboot项目默认不加载application.properties文件

我们都知道,springboot项目启动时会默认把classpath目录下的application.properties文件作为系统配置文件,但如果我们想根据自己的意愿加载别的文件,比如beans.xml、config.xml等等。

设想一个场景,项目中有四个环境的配置,如开发、测试、压测和生产,分别对应下图中标识的四个文件,通过application.properties中的spring.profiles.active属性来指定不同的环境。比如spring.profiles.active=dev说明项目启动时读取的是application-dev.properties中的配置。
在这里插入图片描述

但是,在某种情况下我们不想通过spring.profiles.active来决定加载哪个文件,那么怎么做呢?
可通过下面的代码实现:

@SpringBootApplication
public class SpringbootTestApplication {

	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		//这里指定加载的是application-uat.properties文件的配置
		InputStream inputStream = SpringbootTestApplication.class.getClassLoader().getResourceAsStream("application-uat.properties");
		properties.load(inputStream);
		SpringApplication app = new SpringApplication(SpringbootTestApplication.class);
		app.setDefaultProperties(properties);
		app.run(args);
	}

}

但是,仅仅这样做还不够,我们要把application.properties文件名字修改一下,如果不修改那么无论程序怎么改动,默认加载的还是application.properties文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值