Spring Boot Specify External Startup Configuration File Explanation

  • 2021-11-10 09:42:39
  • OfStack

Directory specifies an external profile-specific configuration summary using the spring boot default profile path

The default package spring boot project will type the configuration file into the jar package, and sometimes you need to modify some configuration items during testing. In addition to overwriting the configuration item with startup parameters, you can also specify an external configuration file to overwrite the existing configuration file. It is convenient when you need to modify more configuration parameters.

Use spring boot default profile path

The default lookup path is as follows:

1.file:./config/

2.file:./

3.classpath:/config/

4.classpath:/

Load will be found from 1 to 4 in turn application.yml Or is it application.properties This configuration file name can be obtained through the spring.config.name Modify), such as ./config/application.yml Will override./application. yml and classpath:/config/applicaiton.yml Configuration with the same name in.

That is, the simplest method is to place a configuration file in the same layer directory of jar package, or in the config subdirectory of the same layer directory, and start jar package to load the configuration file to realize the coverage of configuration items.

Specify an external configuration file

Of course, you can modify the startup parameters spring.config.location To specify the loading directory or file


$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

This does not load the configuration file at the default location, but loads the default.properties And override.properties Documents, override.properties default. properties is overwritten by the configuration with the same name in.

If the specified path ends with/, it is directory configuration, and the configuration file will be found in the directory. The default configuration of this parameter is:


classpath:/,classpath:/config/,file:./,file:./config/

You can compare the default search path above, which is overwritten from right to left.

Specific configuration

In the process of development, testing and release, these three scenarios are relatively fixed, and usually three different configurations application-{profile}. yml are defined, which are switched by profile parameters when used.


applicaiton-dev.yml
applicaiton-test.yml
applicaiton-prd.yml

At startup, specify the spring.profiles.active Parameter to switch configuration files


java -jar myproject.jar --spring.profiles.active=test

Of course, specifying a specific configuration also satisfies the rule of configuration file override, but this parameter switching loses its effect once the specified spring. config. location is not a directory but a specific file.

For more specific extension configuration, please refer to the official document Externalized Configuration of spring boot.

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: