Foreword
SpringBoot has agreed on a configuration file, which defaults to application.properties. Through this file, many default configurations can be modified. Of course, we can also add custom configurations in this configuration file. This file is configured in the form of key = value.
Author | Hengyu Junior-Yu Qiyu
Source | https://blog.yuqiyu.com/spring-boot-basic-load-order-of-config-files.html
1. Doubts about configuration tips?
When we use the development tool to configure, there will be a corresponding prompt, which is entirely due to the spring-configuration-metadata.json configuration metadata file, which records the configuration name , type , and attribution information. If the configuration type is enumeration, selective configuration can also be achieved.
SpringBoot provides a dependency. Its main task is to automatically generate configuration metadata. The name of the dependency is spring-boot-configuration-processor, and a package named spring-configuration-metadata.json will be generated in the META-INF directory when packaging. document.
2. Configuration method
Although the configuration file in the properties format is used by default, this method will cause some prefixes in the configuration to be redundant and less readable. SpringBoot also supports configuration files using the yaml method, which only need to be created in the src / main / resources directory A file named application.yml is sufficient, and it also provides functions when using the configuration.
Two files, application.properties and application.yml, can exist in the project at the same time. After testing, it is found that the property priority will be higher. The configuration with the same name will override the configuration in yml.
3. Specify the configuration file
If the name of your application configuration file is not application, and you want to customize it, you can specify it through the --spring.config.name command line parameter, as follows:
java -jar project-sample.jar --spring.config.name=custome
Note: We only need to specify the name of the configuration file, you can use the properties or yaml file format, the above configuration will load src / main / resources / custome.yml or src / main / resources / custome.properties.
Through --spring.config.name is just to modify the name of the configuration file, so if we modify the directory location of the configuration file, what do we need to do?
SpringBoot is ready for us, you can specify the location of the configuration file through the --spring.config.location parameter, as follows:
java -jar project-sample.jar --spring.config.location=classpath:/configs/custome.yml
If a configuration file can not meet your needs, then you look at the following way:
java -jar project-sample.jar --spring.config.location=classpath:/configs/custome.yml,classpath:/configs/default.properties
Note: Support to specify multiple configuration files through command line parameters, using English half-width, separated.
If you specify not a file but a directory through spring.config.location, be sure to add a "/" at the end of the path, and then combine spring.config.name to configure the configuration file. The combination example is as follows:
# 加载/configs/application.properties 或 /configs/application.yml(默认文件名)java -jar project-sample.jar --spring.config.location=classpath:/configs/# 加载/configs/custome.properties 或 /configs/custome.ymljava -jar project-sample.jar --spring.config.location=classpath:/configs/ --spring.config.name=custome
Note: The default value of the configuration parameter of spring.config.name is application, so if only spring.config.location is specified and it is in the form of a directory, spring.config.name will be automatically appended to the directory path in the above example. Spring.config.location is not a directory, the value of spring.config.name will be ignored here.
4. Load order
The SpringBoot application loads the configuration files in the following order when it starts:
- 1. Configuration file under the class path
- 2. Configuration files in the config subdirectory of the class path
- 3. Configuration files in the root directory of the current project
- 4. Configuration files in the config subdirectory of the current project root directory
The sample project configuration file storage structure is as follows:
. project-sample├── config│ ├── application.yml (4)│ └── src/main/resources| │ ├── application.yml (1)| │ └── config| | │ ├── application.yml (2)├── application.yml (3)
Load configuration file sequence at startup: 1> 2> 3> 4
The configuration files under src / main / resources will be placed under target / classes when the project is compiled.
5. Priority coverage
There is a feature in the SpringBoot configuration file . The loading order of the configuration with higher priority is lower, and the configuration with the same name with higher priority will overwrite the content with lower priority.
To better explain this, we create an application.yml configuration file according to the corresponding loading order to verify whether there is an overlay problem according to the priority, as shown in the following figure:
There is a configuration named name in the above four configuration files, and the content marked in red font is the configuration content of each configuration file name. Let's start the project test and output the content.
6. Run the test
Before testing, we let the startup class implement the CommandLineRunner interface as follows:
@SpringBootApplicationpublic class LoadOrderOfConfigFilesApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(LoadOrderOfConfigFilesApplication.class, args); } @Value("${name}") private String name; @Override public void run(String... args) throws Exception { System.out.println("配置名称:" + name); }}
After the project is started, the content of $ {name} is printed by the run method.
Test one: sequential coverage
Keep the above four configuration files corresponding to the loading order, start the project, and the console outputs:
配置名称:project/config
Expected to be consistent with the actual output, the config directory under the project root is loaded last, so its priority is the highest compared to the other three, and the order of coverage is: 4> 3> 2> 1 .
Test 2: Cross-sequence coverage
In the last test point, we added a configuration file for each loading order. If we only have two configuration files in two project / config and classes / config directories, will we cover them according to priority?
Delete the other two, and only keep the configuration files in the two locations of project / config and classes / config. The output of the startup project console is as follows:
配置名称:project/config
The content of the project / config configuration file with the highest priority is also output in the order of coverage: 4> 1
Test 3: Single order loading
Usually in the project development, the application.yml configuration file is usually placed in the src / main / resources directory. However, according to the above loading order, we can place the configuration file in any place and it will be loaded at startup.
Only the configuration file in the classes / config location is kept, and the output of the startup project console is as follows:
配置名称:classes/config
IDEA's support for Spring Boot is really powerful, and the configuration files under classes / config also provide keyword reminders.
7. Summary
Understand the loading order of configuration files in order to easily configure and overwrite, completely control the use of different configuration content in different environments, remember that classes / application.yml has the lowest priority and project / config / application.yml has the highest priority.
In-depth practice of springboot actual combat PDF