How to add multiple application.properties files in spring-boot?

前端 未结 4 1293
难免孤独
难免孤独 2020-12-05 02:21

I have a Spring boot application that is divided in several modules. The main module runs the application and has an application.properties file in the resource

4条回答
  •  广开言路
    2020-12-05 02:54

    Spring Boot reads the property files in the following order. (From Spring Boot in Action)

    1. Externally, in a /config subdirectory of the directory from which the application is run
    2. Externally, in the directory from which the application is run
    3. Internally, in a package named “config”
    4. Internally, at the root of the classpath

    The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

    So placing application.properties in a config sub-directory will give it a higher priority. In the following configuration, the application.properties from module_a will take precedence. You can add common defaults in application.properties and override them in individual modules by placing the configuration file in config/application.properties.

    +common_module
      +src
        +main
          +java
          +resources/application.properties
    +module_a
      +src
        +main
          +java
            +my/package/Application.java
          +resources/config/application.properties
    

提交回复
热议问题