现在项目基本上是springboot,打包一个jar 直接java命令运行非常方便,那在springboot中如何读取jar外的配置文件呢?
springboot 默认读取外部配置文件顺序:
1.在jar包的同一目录的config文件夹下;
2.jar包的同级目录;
3.在classpath下config文件夹;
4.classpath根目录下
这四种方式的优先级是从一到四一次降低的。
同样你可以使用spring.config.location配置在启动时指定目录位置:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
也可以指定目录,指定目录时要以/结尾
java -jar myproject.jar --spring.config.location=classpath:/config/,classpath:/config1/
当spring.config.location指定了后,如果其包含多个值,优先顺序为从后向前。
比如:配置java -jar myproject.jar --spring.config.location=classpath:/config/,classpath:/config1/
那读取顺序为:
1.classpath:/config1/
2.classpath:/config/
当使用spring.config.additional-location配置时,指定的配置文件要优先于默认配置文件,同样这里的配置生效方式是从后往前。
如果使用环境变量而不是系统属性,大多数操作系统不允许使用点分隔的键名,但是可以使用下划线(例如,SPRING_CONFIG_LOCATION而不是spring.config.location)