Coming from a .NET background, I was looking to emulate configuration transforms. I found some tutorials online but they weren’t totally complete. In this tutorial, we will be creating configuration files for a development tier and a production tier.
First create a new Spring Boot project. You will notice that an application.properties
file was automatically created. For learning purposes, append the following:
test.constant=development_constant
Now create a new file in the same directory as your application.properties
file named application-production.properties
. Append the same key with the “production” value:
test.constant=production_constant
The next step is to create a way to inject these constants into our app. First create the interface:
public interface MyAppConfiguration { String getTestConstant(); }
Then create the implementation:
@Component public class MyAppConfigurationImpl implements MyAppConfiguration { @Value("${test.constant}") private String testConstant; @Override public String getTestConstant() { return testConstant; } }
We have everything setup to run the app in different configuration modes. After generating a fat jar, we can start the Spring Boot app in different configurations via the command line:
java -jar {jar_file_name}.jar # Run jar in development mode. java -jar -Dspring.config.name=application-production {jar_file_name}.jar # Run jar in production mode. nohup java -jar -Dspring.config.name=application-production {jar_file_name}.jar & # Run jar in production mode in background. Requires linux.
I am an Intellij user. Sometimes I want to start the application in a different configuration mode in Intellij. The easiest way to do this is to go to “Run > Edit Configurations > Override parameters”. In the “Name” column, enter “spring.config.name” and then enter the chosen configuration file name in the “Value” column. Here is a sample image which demonstrates this concept:

Evidently, it is incredibly easy to create different configurations per tier. This is not exactly how .NET configuration transforms work but it is close enough.