1、SpringBoot的Jar配置文件放在外面的方式,通过命令指定文件(多个方式):
1> java -jar test.jar --spring.config.location=D:\config\test.properties ,文件路径可以是列表,逗号分隔
2> java -jar -Dspring.config.location=D:\config\test.properties test.jar
2、启动时指定配置文件的后缀(配置文件在jar内的classes下,默认位置):
java -jar test.jar --spring.profiles.active=sit
指定文件名:
java -jar test.jar --spring.config.name=myproject
3、默认读取配置文件的顺序:
- 项目根目录\config\*.properties
- 项目根目录\*.properties
- 项目根目录\src\main\resources\config\*.properties,编译后的路径就是 xxx.jar\BOOT-INF\classes 下
- 项目根目录\src\main\resources\*.properties
注意,假如有多个同名文件,那么高级别的覆盖低级别的,并且保留不冲突的属性。
#####################################
官网列出的顺序:https://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/html/boot-features-external-config.html
- Command line arguments.
- JNDI attributes from java:comp/env.
- Java System properties (System.getProperties()).
- OS environment variables.
- A RandomValuePropertySource that only has properties in random.*.
- Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
- Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
- Application properties outside of your packaged jar (application.properties and YAML variants).
- Application properties packaged inside your jar (application.properties and YAML variants).
- @PropertySource annotations on your @Configuration classes.
- Default properties (specified using SpringApplication.setDefaultProperties).