springboot打包好的Jar文件运行时引用外部的配置文件

去Interview时被问了这个问题,之前都是直接在idea里改好再打包了,所以没有直接引用外部文件过,通过查看SpringBoot官网,得知有下面的几种方法:

24.3 Application Property Files
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

1、A /config subdirectory of the current directory //  大概意思是:放在jar包同级目录下的子目录config
2、The current directory  //大概意思是:放在jar包目录同级
3、A classpath /config package // 大概意思是在classpath下的config目录
4、The classpath root // classpath中

引用的优先级和上面的顺序是一样的。

还有一种是用命令行

java -jar bootdemo-0.0.1-SNAPSHOT.jar --spring.config.location=./test.properties //此时test.properties和jar是同级目录

 我就只用第1种方便:

ok,笔记完毕。 

猜你喜欢

转载自blog.csdn.net/u013845177/article/details/83142106