Where to define properties in SpringBoot which are common across all environments?

Viewed 1004

I have few properties common to all environments (Ex. spring.jpa.properties.hibernate.ejb.interceptor) which I have kept in application.properties under resource directory.

I have DB properties defined in environment based properties file which I pass externally through command line while starting the app:

java -jar -Dspring.config.location=<path-to-file> mySpringBootProject.jar

However, spring.jpa.properties.hibernate.ejb.interceptor is not being set when I am passing properties file externally.

Do I need to define common properties even in external file? Or is there a way I can define them in a single place which is reused when not overridden?

3 Answers

You can use multiple profiles to do this. For example, create property files:

application-dev.properties
application-prod.properties
application.properties

Place your environment-specific properties in the application-${env}.properties file and your common properties in application.properties.

There are multiple ways to tell spring which profiles to use, for example the --spring.profiles.active flag.

See the Spring Boot documentation for more details about the property file search order.

application.properties will normally be overridden by Boot projects. Instead, you can add a properties file in a non-conflicting location (such as src/main/resources/my/package/foo.properties) and use @PropertySource on your autoconfiguration file to add it.

Properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. The advantage of using properties file is, we can configure things which are environment specific (or are prone to change over a period of time) without the need of changing anything in code. Hence inheriting common properties is not a good approach. If a property seems to be static for all e.g. environments, then it shouldn't be a property.

But, it could be we have multiple development environments and production, where we would share same properties in the environments meant for development purposes and different properties for production. In this case we could create a common properties file and inherit it in all our environment specific properties files. Another scenario could be, that at the moment of development the property is same for all environments, but we would like to provide the option of changing it in the future, when required.

Related