使用Spring Boot时,属性按以下顺序加载(请参阅Spring Boot参考指南中的“ 外部化配置 ”)。
- 命令行参数。
- Java系统属性(System.getProperties())。
- 操作系统环境变量。
- 来自java:comp / env的JNDI属性
- 一个RandomValuePropertySource,仅具有random。
*
属性。 - 打包的jar之外的应用程序属性(application.properties,包括YAML和配置文件变体)。
- 打包在jar中的应用程序属性(包括YAML和配置文件变体的application.properties)。
- @Configuration类上的@PropertySource批注。
- 默认属性(使用SpringApplication.setDefaultProperties指定)。
解析属性时(即
@Value("${myprop}")以相反的顺序进行解析(因此从9开始)。要添加其他文件,你可以使用
spring.config.location以逗号分隔的属性文件或文件位置(目录)列表的属性。
-Dspring.config.location=your/config/dir/
上面的一个将添加一个目录,将在该目录中查询
application.properties文件。
-Dspring.config.location=classpath:job1.properties,classpath:job2.properties
这会将2个属性文件添加到已加载的文件中。
默认配置文件和位置在附加指定spring.config.location的文件和位置之前加载,这意味着后者将始终覆盖较早配置文件和位置中设置的属性。(另请参阅《 Spring Boot参考指南》的本节)。
如果
spring.config.location包含目录(而不是文件),则目录应以/结尾(并
spring.config.name在加载后附加从生成的名称)。
classpath:,classpath:/config,file:,file:config/始终使用默认搜索路径,而与的值无关
spring.config.location。这样,你可以在中设置应用程序的默认值
application.properties(或使用来选择的其他任何基本名称
spring.config.name),并在运行时使用其他文件覆盖它,并保持默认值。
更新:由于spring.config.location的行为现在将覆盖默认值,而不是添加至默认值。你需要使用spring.config.additional-location来保持默认值。这是从1.x到2.x的行为更改



