Home  >  Article  >  Java  >  What are the default loading paths of springboot?

What are the default loading paths of springboot?

王林
王林forward
2023-05-15 22:16:041202browse

1. First introduction to the application.properties/.yml file

As mentioned earlier, the application.properties file is not configured. Now I create a new application.properties file under the main/resources folder and write the following Configuration

What are the default loading paths of springboot?

Then, start the project,

What are the default loading paths of springboot?

Okay, through the screenshot of the startup result above, you can see the service Started on port "9090". Then if you configure an application.yml file in this directory,

What are the default loading paths of springboot?

The startup result is as follows,

What are the default loading paths of springboot?

Okay You can see that the service is still started on port "9090". For this reason, we draw this conclusion: If there are two files application.properties and application.yml in the same folder, then the application.properties file will overwrite the application.yml file, and the application.properties file will take effect.

What is a yml file

yml is a file format written in YAML (YAML Aint Markup Language), which is a very intuitive data serialization method. It is very simply a new file format, similar to XML and properties files.

2. Can the application.properties/.yml file be in other paths?

Friends who see this title will definitely say, can application.properties be in other paths? Yes, guess correct. We put application.properties/.yml in the main/resources folder. When the project is running, the files in this folder will be copied to the classes folder, so this path is also called classpath.

Search for "application.properties" in the springboot source code and find a class like ConfigFileApplicationListener. There is the following paragraph in the comments of this class.

* {@link EnvironmentPostProcessor} that configures the context environment by loading
 * properties from well known file locations. By default properties will be loaded from
 * 'application.properties' and/or 'application.yml' files in the following locations:
 * <ul>
 * <li>file:./config/</li>
 * <li>file:./config/{@literal *}/</li>
 * <li>file:./</li>
 * <li>classpath:config/</li>
 * <li>classpath:</li>
 * </ul>

It generally means "EnvironmentPostProcessor passes Load properties from well-known file paths to configure the context. The default properties come from the application.properties/application.yml file, which come from the following path ",

file:./config/ root folder (project file config directory under the root file folder (project folder)

file:./config/{@literal *}/ config/*/ directory under the root file folder (project folder)

file:./ Under the root file folder (project folder)

classpath:config/ config/

classpath under classpath: under classpath

Okay, we already know application. The properties/application.yml file will be loaded from the above locations, and the main/resources we configured is actually the above classpath:, so are the above paths also in order? Continue reading along the ConfigFileApplicationListener class. , I saw such an attribute below,

// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/*/,file:./config/";

You can see that DEFAULT_SEARCH_LOCATIONS specifies the above values, and the above comments are very interesting, but I didn't translate it well. Let's verify it through examples. Now that we know the priorities of application.properties and application.yml, we can just use the application.properties file to verify it. There is application.properties under resources/config (running path: classpath:/config/),

What are the default loading paths of springboot?

Look at which port is being used now,

What are the default loading paths of springboot?

is using 9092, which means the priority of resouces/config Greater than resources, that is, classpath:/config/>classpath:/. Let's take a look at file:/

What are the default loading paths of springboot?

The application.properties file is created under the root path of the project. The starting server.port is 9093. Let's see which port is used by the service.

What are the default loading paths of springboot?

The service uses 9093, and the priority is: file:/>classpath:/config/>classpath:/. Now I add the config/my/ path to the project path and add the application.properties file.

What are the default loading paths of springboot?

##The test results are as follows,

What are the default loading paths of springboot?

The service is started on port "9094", indicating the priority is: file:./config/*/>file:./>classpath:/>classpath:/config/, the last verification point is file :/config, I think this does not need to be verified and must have the highest priority. Therefore, the priority order from high to low is: file:./config/ > file:./config/*/ > file:./ > classpath:/ > classpath:/config/, in layman’s terms: config under the project root path>config/*/ under the project root path>project root path>classpath:/config>classpath:/

The above is the detailed content of What are the default loading paths of springboot?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete