Development Environments in Spring Boot

Anthony Dreessen
2 min readNov 29, 2020

This is a short tutorial on how to use different development environments in the Spring Boot framework

Why?

Skip this part if you already know why you might want environments.

Environments allow you configurability in the way your application is launched. This means that you can host our application in different location / on different infrastructure without changing the underlying logic of the application. For example, you might have a staging environment that isn’t public facing. It probably contains fake data and developers can use it to make sure things aren’t broken. You might have development environment that runs on your local machine. And finally, you have your production environment that the customer uses and sees.

Environments allow you to make the differences between these configurations explicit and automatable. Using environments, you can use CI/CD pipelines to automatically deploy to staging or production toggled based off of your chosen git branch.

How?

Environments in Spring are set up using the application.properties file. The typical filepath is src/main/java/resources/application.properties

This file might already exist in which case you’re already using environments, though likely just the initial (default) environment.

In order to make additional environments

  1. Make an additional application.properties file with the name of the environment appended to the end of “application-”

For example: application-prod.properties or application-staging.properties

Now, when you want to launch the application using a different environment

2. Pass the argument -Dspring.profiles.active=<environment_name> to the command line Java process you’re using to do whatever action it is that you are wanting to do.

Full Step-By-Step instructions

  1. Create a new file called application-foo.properties located next to the other application.properties files
  2. Copy-paste the default application.properties file into the new one
  3. Change whichever value you want to change in the new environment
  4. Package the application into a jarfile (gradle bootJar)
  5. Run the application using java -jar -Dspring.profiles.active=foo my-jar.jar

Verify that your environment has changed and you should be good to go.

--

--

Anthony Dreessen

Full sack engineer with an ethic of “do more with less”. Avid gardener and creator of Plenti (plenti.dev)