Knowledge changes fate, stroke code makes me happy, 2020 continue to walk in the open source community click “like” to see, form a habit to give me a Star, click to understand the componentialized interface services based on SpringBoot landing solution

SpringBoot provides a configuration file, the default is application.properties, which allows you to modify many of the default configurations. Of course, you can add custom configurations to the configuration file, which is configured by key=value.

Recommended reading

  • Springboot2.x tutorial summary

Confused configuration prompts?

The spring-configuration-metadata.json configuration metadata file records the name, type, and class of the configuration. If the configuration type is enumeration, you can also implement optional configuration.

SpringBoot provides a dependency whose main task is to automatically generate configuration metadata. This dependency is named spring-boot-configuration-processor. A file named spring-configuration-metadata.json is generated in the meta-INF directory during packaging.

Configuration mode

The default configuration file in the properties format is used, but this method will lead to redundant prefixes and poor readability. SpringBoot also supports yamL configuration files. Just create a file named application.yml in the SRC /main/resources directory.

Properties and application.yml files can exist in the project at the same time. After testing, it is found that the properties file has a higher priority, and the configuration with the same name will override the configuration in YML.

Specifying a configuration file

If the name of your application configuration file is not Application and you want to customize it, you can specify it with the –spring.config.name command line argument, as shown below:

java -jar project-sample.jar --spring.config.name=custome
Copy the code

** Notes: * * we only need to specify the configuration file name, you can use the properties or yaml file format, the configuration above will load the SRC/main/resources/custome yml or SRC/main/resources/custome properties.

Using –spring.config.name only changes the name of the configuration file, so what do we need to do to change the directory location of the configuration file?

SpringBoot is ready for us to use the –spring.config.location parameter to specify the location of the configuration file, as shown below:

java -jar project-sample.jar --spring.config.location=classpath:/configs/custome.yml
Copy the code

If a configuration file doesn’t meet your needs, look at this:

java -jar project-sample.jar --spring.config.location=classpath:/configs/custome.yml,classpath:/configs/default.properties
Copy the code

** Note: ** supports the use of command line parameters to specify multiple configuration files, using English half corner, separate.

If you specify a directory instead of a file via spring.config.location, be sure to add a “/” at the end of the path and combine the configuration files with spring.config.name as shown in the following example:

# loading/configs/application. The properties or/configs/application. Yml (the default file name)
java -jar project-sample.jar --spring.config.location=classpath:/configs/

# loading/configs/custome properties or/configs/custome. Yml
java -jar project-sample.jar --spring.config.location=classpath:/configs/ --spring.config.name=custome

Copy the code

Matters needing attention: The default value of this configuration parameter is application, so if you specify spring.config.location as a directory, the example above automatically appends spring.config.name to the directory path. If the specified spring.config.location is not a directory, the value of spring.config.name is ignored here.

Load order

SpringBoot applications load configuration files in the following order when they start up:

  1. Configuration file in the classpath
  2. Configuration file of the config subdirectory in the classpath
  3. Configuration file in the root directory of the current project
  4. Configuration file of the config subdirectory in the current project root directory

The sample project configuration file storage structure is as follows:

.project - sample ├ ─ ─ the config │ ├ ─ ─ application. Yml (4) │ └ ─ ─ SRC/main/resources | │ ├ ─ ─ application. Yml (1) | │ └ ─ ─ The config | | │ ├ ─ ─ application. Yml (2) ├ ─ ─ application. Yml (3)Copy the code

The sequence of loading configuration files during startup is 1 > 2 > 3 > 4

Configuration files under SRC /main/resources will be placed under target/classes when the project is compiled.

Priority coverage

The SpringBoot configuration file has a feature that the configuration with a higher priority is loaded later. The configuration with the same name with a higher priority overwrites the configuration with a lower priority.

To better explain this, we create an application.yml configuration file respectively according to the corresponding loading order to verify whether there is an overwriting problem according to the different priorities, as shown in the figure below:

There is a configuration named name in the above four configuration files, and the content marked in red is the configuration content of each configuration file name. Let’s start the project test and output the content.

Run the test

Before testing we let the startup class implement the CommandLineRunner interface, as shown below:

@SpringBootApplication
public 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("Configuration Name:"+ name); }}Copy the code

After the project is started, the run method is used to print the content of ${name} configuration.

Test 1: sequential coverage

Keep the above four configuration files corresponding to the loading order, start the project, console output content:

Configuration name: project/configCopy the code

Expected to match the actual output, the config directory at the root of the project is loaded last, so it has the highest priority relative to the other three, overwritten in order: 4 > 3 > 2 > 1.

Test two: cross-order coverage

At the last test point, we added a configuration file for each load order. If we only had two configuration files for project/config and classes/config, would we overwrite them by priority?

Delete the other two, leaving only the configuration files in the project/config and classes/config locations, and start the project console output as follows:

Configuration name: project/configCopy the code

It also outputs the contents of the project/config configuration file with the highest priority in the order of 4 > 1

Test point: single-order loading

Yml configuration files in the SRC /main/resources directory are normally placed in the application. However, according to the loading order above, we can place the configuration files in any place and load them at startup.

Just keep the configuration files for the classes/config location, and the output from the startup project console looks like this:

Configuration name: classes/configCopy the code

The IDEA support for SpringBoot is really strong, and the classes/config configuration file also provides keyword alerts.

conclusion

Understand configuration file load order to handy configured coverage, complete control in the different environment with different configuration, to remember the classes/application. The yml, lowest priority project/config/application. The yml highest priority.

Author’s personal blog uses the open source framework ApiBoot to help you become an Api service architect