Manage application.properties for different environments for java application without spring boot

Manoj Mohan
Jun 20 · 1 min read

We wanted to manage different configurations like database URL, API keys for different environments. When we went for help with Google, all we saw was how to do it on spring boot applications. After a complete day of work, we were able to set it up. So, below are the steps

Our application was built on spring 4 and Java 1.8

Before:

import org.springframework.context.annotation.PropertySource;@PropertySource(value = “classpath:application.properties”)// Access value like belowdataSource.setUsername(environment.getRequiredProperty("jdbc.username"));

Now:

import org.springframework.context.annotation.PropertySources;
import org.springframework.context.annotation.PropertySource;
@PropertySources({@PropertySource(value = “classpath:application.properties”),@PropertySource(value=”classpath:application.beta.properties”, ignoreResourceNotFound=true),@PropertySource(value=”classpath:application.prod.properties”, ignoreResourceNotFound=true)})dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));

Then what?

Add application.prod.properties and application.beta.properties to .gitignore. So that production and beta configurations will be out of VCS.

Manually create these configs in beta and prod environments. So, When application.*.properties exists these will override the application.properties.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade