springboot配置文件加载顺序
springboot配置文件加载顺序
官方文档如下说明:
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
2. The current directory
3. A classpath /config package
4. The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
You can also use YAML (’.yml’) files as an alternative to ‘.properties’.
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:
$ java -jar myproject.jar --spring.config.name=myproject
The following example shows how to specify two locations:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
spring.config.name and spring.config.location are used very early to determine which files have to be loaded. They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).
If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.
Config locations are searched in reverse order. By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/. The resulting search order is the following:
1 file:./config/
2 file:./
3 classpath:/config/
4 classpath:/
When custom config locations are configured by using spring.config.location, they replace the default locations. For example, if spring.config.location is configured with the value classpath:/custom-config/,file:./custom-config/, the search order becomes the following:
1 file:./custom-config/
2 classpath:custom-config/
Alternatively, when custom config locations are configured by using spring.config.additional-location, they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations of classpath:/custom-config/,file:./custom-config/ are configured, the search order becomes the following:
1 file:./custom-config/
2 classpath:custom-config/
3 file:./config/
4 file:./
5 classpath:/config/
6 bclasspath:/
This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties (or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.
If you use environment variables rather than system properties, most operating systems disallow period-separated key names, but you can use underscores instead (for example, SPRING_CONFIG_NAME instead of spring.config.name).
If your application runs in a container, then JNDI properties (in java:comp/env) or servlet context initialization parameters can be used instead of, or as well as, environment variables or system properties.
工具翻译后为:
SpringApplication从application.properties以下位置的文件加载属性并将其添加到Spring中Environment:
1. 一个/config当前目录的子目录
2. 当前目录
3. 类路径/config包
4. 类路径根
该列表按优先级排序(在列表较高位置定义的属性会覆盖在较低位置定义的属性)。
您还可以使用YAML(.yml)文件来替代.properties。
如果您不喜欢application.properties配置文件名,则可以通过指定spring.config.name环境属性来切换到另一个文件名。您还可以使用spring.config.location环境属性(目录位置或文件路径的逗号分隔列表)来引用显式位置。下面的示例演示如何指定其他文件名:
$ java -jar myproject.jar --spring.config.name = myproject
下面的示例演示如何指定两个位置:
$ java -jar myproject.jar --spring.config.location = classpath:/default.properties,classpath:/override.properties
spring.config.name并且spring.config.location很早就用于确定必须加载哪些文件。必须将它们定义为环境属性(通常是OS环境变量,系统属性或命令行参数)。
如果spring.config.location包含目录(而不是文件),则应以目录结尾/(并且在运行时,在目录后附加从生成spring.config.name之前生成的名称,包括特定于配置文件的文件名)。指定的文件spring.config.location按原样使用,不支持特定于配置文件的变体,并且被任何特定于配置文件的属性覆盖。
配置位置以相反的顺序搜索。默认情况下,配置的位置是classpath:/,classpath:/config/,file:./,file:./config/。结果搜索顺序如下:
1 file:./config/
2 file:./
3 classpath:/config/
4 classpath:/
当使用来配置自定义配置位置时spring.config.location,它们将替换默认位置。例如,如果spring.config.location使用值配置classpath:/custom-config/,file:./custom-config/,则搜索顺序将变为以下内容:
1 file:./custom-config/
2 classpath:custom-config/
另外,当使用来配置自定义配置位置时spring.config.additional-location,除默认位置外,还会使用它们。在默认位置之前搜索其他位置。例如,如果classpath:/custom-config/,file:./custom-config/配置了的其他位置,则搜索顺序变为以下内容:
1 file:./custom-config/
2 classpath:custom-config/
3 file:./config/
4 file:./
5 classpath:/config/
6 classpath:/
通过此搜索顺序,您可以在一个配置文件中指定默认值,然后在另一个配置文件中有选择地覆盖这些值。您可以在以下默认位置之一中为您的应用程序提供默认值application.properties(或使用来选择的其他任何基本名称spring.config.name)。然后,可以在运行时使用自定义位置之一中的其他文件覆盖这些默认值。
如果您使用环境变量而不是系统属性,则大多数操作系统都不允许使用句点分隔的键名,但是您可以使用下划线代替(例如,SPRING_CONFIG_NAME代替spring.config.name)。
如果您的应用程序在容器中运行,则可以使用JNDI属性(中的java:comp/env)或Servlet上下文初始化参数来代替环境变量或系统属性,也可以使用它们。