Home  >  Article  >  Java  >  How does SpringBoot hang the configuration file outside the jar package?

How does SpringBoot hang the configuration file outside the jar package?

WBOY
WBOYforward
2023-05-15 15:01:064568browse

    1. SpringBoot specifies the configuration file path:

    In SpringBoot, you can place the configuration file outside the jar package, so that you can easily modify the configuration. No repackaging and deployment required.

    The following are several ways to specify the configuration file directory: (sorted by effective priority from high to low)

    1) Use command line parameters:

    When starting the application, you can use the --spring.config.location or -Dspring.config.location command line parameter to specify the path to the configuration file, for example: ( The / after config cannot be omitted)

    java -jar test.jar --spring.config.location=/opt/app/config/

    Supplement: FHS defines /opt as "reserved for the installation of additional application packages." So the /opt folder is selected here to store the application configuration file.

    2) Use environment variables:

    You can set the SPRING_CONFIG_LOCATION environment variable to specify the path to the configuration file, for example:

    export SPRING_CONFIG_LOCATION=/opt/app/config/
    java -jar test.jar

    3) Use external configuration File:

    • Method 1: Create a file named config# in the application (jar package) startup command execution directory ## folder, and then place the configuration file in that folder.

    • Method 2: Create a file named config in the directory of the same level as the application (jar package) folder and place the configuration file in that folder. Add the following lines in your code's configuration file application.properties or application.yml:

      spring.config.name=application
      spring.config.location=classpath:/,file:./config/
    • Method 3:

      Directly place the properties or yml configuration file in the application (jar package) directory at the same level.

    • Method 4:
    • Create

      config folder, and then put the properties or yml configuration file. Method 5: In the same level directory of

      classpath
    • inside the application (jar package)
    • , and then Directly put the

      properties or yml configuration file. Note: The above methods can be used in combination. For example, you can specify the path to the configuration file in both command line parameters and environment variables. The command line parameters have the highest priority during execution. 2. The priority ordering for the SpringBoot configuration file to take effect:

    java -jar

    In the command line, pass

    --spring. The method specified by the config.location

    parameter has the highest priority.
    1. Setting the SPRING_CONFIG_LOCATION environment variable to specify the path to the configuration file is secondary priority.

    2. The

      config folder in the same directory as the java -jar

      command is the third priority.
    3. Note: If the java -jar command is not executed in the same directory as the project jar package, config# in the same directory as the project jar package ## The folder is not valid.

    4. The properties or yml file in the project jar package specifies the

      spring.config.location
    5. configuration folder. Four priorities.
    6. Put the project jar package directly under the same level. properties The file is the fifth priority. Directly put the configuration file into the directory at the same level as the jar package.

    7. The
    8. classpath

      sibling config folder inside the project is the sixth priority. Create a config folder under the classpath, and then put Put the configuration file in.

    9. classpath within the project is placed at the same level. properties files have the lowest priority and are placed directly under

      classpath
    10. configuration file.
    11. SpringBoot defaults to reading a config/application.properties file in the same directory as the java -jar command first.

    12. The
    application.properties

    file created under the src/main/resources folder within the project has the lowest priority.

    The above is the detailed content of How does SpringBoot hang the configuration file outside the jar package?. 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