Home  >  Article  >  Java  >  How does springboot find the order and priority of configuration file paths?

How does springboot find the order and priority of configuration file paths?

WBOY
WBOYforward
2023-05-26 08:58:182552browse

    The order in which configuration file paths are searched and their priorities

    1. The order in which springboot searches for configuration file paths and their priorities

    In the project class, there are four places that the system will automatically scan:

    1.file:/config/

    2.file:/

    3.classPath:/config/

    4.classPath:/

    1) Its file validity priority level is from top to bottom, that is, if there are the same attributes in different paths, the path attributes above will be selected first (1>2> ;3>4)

    2) The path scanning order is opposite to its priority (4>3>2>1). The later ones will overwrite the previous ones, and all paths will be scanned, forming a complement.

    2. Under the same path

    application.properties(application.yml), bootstrap.properties(bootstrap.yml) loading order and priority

    1. Comparison between application and bootstrap (prefix comparison)

    Tip: Bootstrap type files will only take effect when springcloud related configuration is introduced in multi-service projects. Springboot will not automatically scan The bootstrap file will only scan the application file, so the comparison between the two is only meaningful in the spingcloud multi-service project.

    1) Loading order: If application and bootstrap are in the same directory: bootstrap loads application first and then loads

    Bootstrap properties have higher priority. By default, they will not Overridden by local configuration. (This is what the Internet and the official say, but after my own testing, I found that application will override bootstrap. My understanding is that all bootstrap attributes have priority over application. Only the configuration related to the underlying system will be higher. There are different opinions. Brother, welcome to discuss)

    3) Difference: Both bootstrap and application can be used to configure parameters.

    Bootstrap is used for early application boot and configuration information reading. It can be understood as some parameter configurations at the system level. These parameters generally do not change. Once bootstrap is loaded, the content will not be overwritten.

    application.yml can be used to define application-level, application-specific configuration information, and can be used to configure public parameters to be used in subsequent modules, etc.

    4) Typical application scenarios of bootstrap (classic three points)

    • 1. When using the Spring Cloud Config Server configuration center, you need to specify it in bootstrap.yml In the spring.applicatio configuration file n.name and spring.cloud.config.server.git.uri, add the configuration properties connected to the configuration center to load the configuration information of the external configuration center

    • 2. Some fixed properties that cannot be overwritten

    • 3. Some encryption/decryption scenarios

    2. .properties and .yml comparison (suffix comparison)

    1) Loading order

    .yml precedes .properties

    2)Priority level

    .properties Prior to .yml, because .properties will overwrite .yml when loaded later

    3) Difference

    • 1.yml has a natural tree structure, and properties are key-value pairs ;

    • 2. The properties file is separated by ".", and the .yml is separated by ":";

    • 3. You must not use TAB for indentation in .yml, otherwise a very strange error will be reported;;

    • 4.yml is more friendly to Chinese support than .properties.

    • 5.yml is cross-language: it can be used in a large number of languages ​​including JAVA, go, python, etc. For example, when using go and java for cloud computing, you can use the configuration center Use the same configuration!

    • 6. When customizing the configuration file, you can only use properties, because spring only has the @PropertiesSource annotation that can introduce custom annotations and has no support for yml

    Summary: In the same level directory, the priority level application>bootstrap,.properties>,yml, the prefix is ​​greater than the suffix, and the file path is greater than the file name

    The reason why the springboot configuration file does not take effect

    1. Change the packaging method to jar.

    <packaging>jar</packaging>

    2. It may be that the shortened format of the yml file is incorrect, just check it carefully.

    3. Also, yours is not a boot project at all, application.yml does not have a leaf mark

    1. Click on the project structure

    How does springboot find the order and priority of configuration file paths?

    ##2. Open the module and click once

    How does springboot find the order and priority of configuration file paths?

    3.

    How does springboot find the order and priority of configuration file paths?##4.Refactor the project

    The above is the detailed content of How does springboot find the order and priority of configuration file paths?. 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