SpringBoot internal and external configuration file loading and priority

1. Introduction

Foreword:

One of the important thoughts of such a meteor as Spring is: the thought of configuration. So as to achieve the purpose of decoupling, many do not need to be hard-coded and can be completed with a configuration, which can be said to greatly improve the coding efficiency.

There are many ways to load configuration files supported by Spring . On the one hand, it expands flexibility, but on the other hand, it also brings greater complexity. Therefore, this article aims to use an article to solve the troubles of readers and friends, and sometimes it is also your troubles.

We know that SpringBoot supports three configuration files:

application*.yml  
application*.yaml   
application*.properties 

Example:

Create application.yml  and  application.properties  files in the  Resources  folder  .

application.properties:

server.port=8001

application.yml  :

server:
  port: 7001

Run the program:

Through the running results, we can get:

These three configuration files have the  highest priority of application*.properties  . When application.properties  and  yml files coexist (under the same directory) ,

application.properties The priority is better, it will be read first, if it is not, then the value in yml will be read.

All the demo codes below are demonstrated  in  yml 

Two, internal configuration file loading

 What are the directories for loading internal configuration files?

We found the class ConfigFileApplicationListener to  open in the IDE editor, and found  that there are four default scan directories:

 The corresponding four directories:

path   Scan priority
 ./  The config directory under the project root directory 2
 ./config The config directory under the project root directory  1
 classPath:./ The root directory under the build path  4
 classPath:/config The config directory under the compilation path  3

 

 

 

 

Scanning sequence after project start:

    1. First go to the project root directory to find the configuration file in the config folder
    2. Go to the root directory to find the configuration file
    3. Go to resources to find the configuration file in the cofnig folder
    4. Go to resources to find the configuration file

 For the above four directories, all configuration files will be loaded in the order of priority from low to high when SpringBoot is  started, and high-priority configuration files will overwrite low-priority configuration files.

SpringBoot  will load configuration files in all four locations. If the properties of the high-priority configuration file and the low-priority configuration file do not conflict, they will coexist and complement each other.  

According to the above directory, we create the corresponding folder and the files under the folder in the project. The priority of the configuration file is:

Remarks:

The configuration files mentioned here are still in the project. In the end, they will be included in the jar package, so be careful.

1. If there is application.yml  and application.properties in the same directory,  application.properties is read first by default.
2. If the same configuration attribute is configured in multiple configuration files, the first read is used by default, and the later read does not overwrite the previous read.
3. When creating a SpringBoot project, the general configuration file is placed in the "resources directory of the project"

 

Three, external configuration file loading

Sometimes, the configuration information cannot be determined during the development process. For example, for projects developed for customers, customers need to customize the configuration according to their own conditions, such as database configuration, encryption key configuration and so on.

At this time, you need to put the configuration file outside to allow users to customize the configuration and deployment.

When SpringBoot is packaged, the ./config ./ two-level directories will not be packaged. This design is very clever, because it is not packaged, SpringBoot can load external configuration files.

SpringBoot supports storing configuration files externally. As long as the configuration file is placed in the same level directory of the jar package, or in the config folder under the same level , SpringBoot will read the configuration file here.

1. Now there is a SpringBoot  packaging program (there is a configuration file application.yml in the default  Resources  folder):

 application.yml  configuration information:

server:
  port: 8001

Execute the command java -jar springboot-0.0.1-SNAPSHOT.jar, after the program is started, we can see that the startup port is 8001 that we configured:

 2. At this time, we add the configuration file application.properties to the root directory  :

server.port=8002

Execute the command   java -jar springboot-0.0.1-SNAPSHOT.jar   , after the program is started, the starting port is  port 8002  configured in the application.properties  configuration file 

3. At this time, we add a folder and configuration file ./config/application.properties to the root directory

server.port=8003

Execute the command java -jar springboot-0.0.1-SNAPSHOT.jar   , after the program is started, we find that the startup port is port 8003   configured in the  application.properties configuration file in the  root directory  config folder  

 

 In this way, you only need to modify the configuration file information every time you modify it, and then restart the project, without repackaging. If you want to modify only the configuration file and it will take effect without restarting the project, you can consider using java to read the data in the configuration file.

Four, command line configuration mode

For example, in the method of  java -jar , multiple properties are separated by spaces, and the properties in properties can basically be used. The usage is the method of property item = property value, for example:   

java -jar springboot-0.0.1-SNAPSHOT.jar --server.port=8002 --server.context-path=/example

The above command line has two configurations server.port and server.context-path. It can be configured as required.

If our external configuration file does not want to be placed in the default location , we can use the  --spring.config.location  command to specify the directory of the configuration file .

Create a new test  folder in the root directory  and put the application.properties configuration file in it:

The  content of the application.properties configuration file in the test folder   :

server.port=8006

Execute the following command line to start the project:

java -jar springboot-0.0.1-SNAPSHOT.jar --spring.config.location=./test/application.properties

After the project is started, you can see that the port is  the port configured in the application.properties configuration file in the test  folder, and the configuration takes effect.

 

Original address:

https://www.cnblogs.com/wwj1992/articles/12569164.html

 

 

 

Guess you like

Origin blog.csdn.net/llwy1428/article/details/106827040