Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 845
mat_boy Avatar asked Oct 20 '15 06:10

mat_boy


People also ask

Can we have more than one application properties in spring boot?

Spring Boot supports different properties based on the Spring active profile. For example, we can keep two separate files for development and production to run the Spring Boot application.

Where do you put all the properties of your application for spring boot?

Spring Boot Framework comes with a built-in mechanism for application configuration using a file called application. properties. It is located inside the src/main/resources folder, as shown in the following figure.

How do I read multiple values from application properties in spring boot?

We use a singleton bean whose properties map to entries in 3 separate properties files. This is the main class to read properties from multiple properties files into an object in Spring Boot. We use @PropertySource and locations of the files in the classpath. The application.


1 Answers

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)

like image 78
geoand Avatar answered Oct 04 '22 13:10

geoand