Mukul Goel
Mukul Goel

Reputation: 8465

Maintain multiple properties files in Spring Boot application

I have a spring boot application and it has multiple property files, one for each environment local, dev, test, performance, production.

Recently we had an incident where someone forgot to put a new property entry into the production property file and the system was down for hours in production.

My question is, are there any better practices on how to maintain properties across multiple property files?

I am right now thinking of a utility, maybe a unit test that verifies each key exists in each property file and it would fail the build if it does not exist.

I did a few searches but couldn't really find much on it. I understand SO question terms and that I am kind of seeking opinion here. I am more looking to know the better practices on maintaining property files.

Upvotes: 2

Views: 3577

Answers (3)

Humble Freak
Humble Freak

Reputation: 459

You should use different properties files for different environments.like

application-qa.properties for QA environment

application-prod.properties for Production Environment

application-local.properties for Local Environment

And then use multiple scripts to start the application for different environments, like each shell script would contain

mvn spring-boot:run -Drun.profiles=qa

mvn spring-boot:run -Drun.profiles=prod

mvn spring-boot:run -Drun.profiles=local

For example application-local.properties file is:

spring.profiles.active=local

spring.data.mongodb.host=localhost

spring.data.mongodb.port=27017

spring.data.mongodb.database=my_db_name

spring.data.mongodb.username=some_user_name

spring.data.mongodb.password=some_password

spring.rabbitmq.host=localhost

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

spring.rabbitmq.port=5672

rabbitmq.publish=true

Upvotes: 4

Phanindra Gopishetty
Phanindra Gopishetty

Reputation: 169

You can use a default properties file. If there is no property in a particular env specific file then it will look into this properties file.

wherever possible while resolving property in spring try providing default value. For eg. @Value("${mongodb.url:127.0.0.1}")

Upvotes: 1

Akhil
Akhil

Reputation: 1274

Yeah, that's standard practice followed to separate environment dependent properties and have them injected using spring.profile.active, but the scenario you described seems more to be human error and can occur with any utility, one can have test to verify properties but it introduces maintainability issue. I prefer to manage production related properties using spring cloud config (here) because production properties may contain db credentials and secret keys which may get leaked.

Upvotes: 1

Related Questions