Spring Boot different environments

125 Views Asked by At

I have a spring boot application, but the development environment has a different configuration from the production one (example: MySQL password). I would like to be able to distinguish the properties files, so that I can compile once and be able to launch the application both locally and in production, specifying the profile for the environment. How can I do this thing?

1

There are 1 best solutions below

0
Ken Chan On

Define properties that are the same in all environments in application.properties. For properties that are only specified to a particular environment , define them in their profile specific properties file such as application-prod.properties or application-dev.properties. prod and dev are the profile name that you can freely define as you like.

Profile specific properties will override application.properties.

To enable a particular profile , you can specify it through the JVM system property spring.profiles.active likes :

java -jar -Dspring.profiles.active=prod app.jar

Or through OS environment variable :

export SPRING_PROFILES_ACTIVE=prod

See this and this for details