mat_boy
mat_boy

Reputation: 13666

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

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 resources folder. I'm wondering if I can add another properties file in a submodule to separate properties that are belonging to that module and how to make this working (because it is not).

+main_module
  +src
    +main
      +java
        +my/package/Application.java
        +resources/application.properties
+support_module
  +src
    +main
      +java
        +resources/application.properties

So, this is the current situation. Clearly the properties file in the module support_module is not read causing a NoSuchBeanDefinitionException, while if I put the content in the other properties file everything works fine.

Upvotes: 65

Views: 108875

Answers (7)

rupweb
rupweb

Reputation: 3328

I found that spring.config.import in my application.properties brought in the additional properties file.

spring.config.import=file:./src/main/resources/application-local.properties

Upvotes: 1

Vivek Adhikari
Vivek Adhikari

Reputation: 237

Multiple application.properties is not possible in same artifacts but you can have

 application.properties 
 application.yml

in the same artifacts. This may help to separate concerns.

Upvotes: 0

Silk0vsky
Silk0vsky

Reputation: 1032

As an option you can design your modules more properly and implement EnvironmentPostProcessor to load they personal properties files automatically.

Please see my detailed answer: How to inherit application.properties in Spring?

Upvotes: 0

zeronone
zeronone

Reputation: 3041

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

Upvotes: 11

Imtiaz Shakil Siddique
Imtiaz Shakil Siddique

Reputation: 4298

The problem is exactly what @geoand describes. Spring boot loads top level application.properties and ignores any properties file with the exact name located in other jars.

But I didn't find any concrete implementation on how to fix this problem, so here it is for those who wants to know the implementation.

Consider this project configuration:

+main_module
  +src
    +main
      +java
        +my/package/Application.java
      +resources/application.properties
+module_aa
  +src
    +main
      +java
        +my/package/config/ModuleAAConfig.java
      +resources/module_aa.properties
+module_bb
  +src
    +main
      +java
        +my/package/config/ModuleBBConfig.java
      +resources/module_bb.properties

Now to load properties for each sub modules correctly we need to add @PropertySource annotation on the configs of each module i.e ModuleAAConfig.java, ModuleBBConfig.java.

Example:

ModuleAAConfig.java

package my.package.config;

@Configuration
@PropertySource(
    ignoreResourceNotFound = false,
    value = "classpath:module_aa.properties")
public class ModuleAAConfig {}

ModuleBBConfig.java

package my.package.config;

@Configuration
@PropertySource(
    ignoreResourceNotFound = false,
    value = "classpath:module_bb.properties")
public class ModuleBBConfig {}

Bonus:

If you want to load profile specific property, you can do so by utilizing spring variables e.g.

@PropertySource("classpath:module_aa-${spring.profiles.active}.properties")

Upvotes: 18

srsajid
srsajid

Reputation: 1787

You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths)

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

For more information click here

Upvotes: 9

geoand
geoand

Reputation: 63991

What you are trying to do will not work when using Maven or Gradle. The reason is that when the artifact (jar most likely since you are using Spring Boot) is created, there will only be one application.properties file in the root.

I suggest you either change the name of the properties file on the support module and then configure Spring Boot to look for that file as well (take a look at this or this answer for pointers), or use some kind of merging task for your build tool (something like this perhaps)

Upvotes: 28

Related Questions