Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java Spring boot Configuration settings for different environment

I am using maven with java Spring-boot.

How can efficiently handle configuration for different environment?
For instance, if I am on staging use this port, if I am on dev, use this port

Currently, I change all the necessary settings in the application.properties each time I move to a different environment

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can create different application.properties file for different environment. The environment name should be appended to the filename and separated with dash (-). For example,

you can have application-dev.properties for dev environment , you can also have ‘application-staging.propertiesfor staging environmentapplication-prod.properties` for production environment.

To activate each environment, pass it as a flag when are starting your .jar application for example

java -jar myapp.jar --spring.profile.active=staging

or you can define the active profile inside application.properties.

I.e (In the example below , your application will make use of the configuration inside application-staging.properties )

spring.profiles.active=staging #comment out to switch away from staging
#spring.profiles.active=dev # uncomment to set active profile to dev
#spring.profiles.active=prod # uncomment to set active profile to prod

application.properties configuration

You can now have different settings based on different environment in the custom created files.

For instance the staging environment could be configured to start on port 8888 as shown below
application-staging.properties content

and the dev could be configured to start on port 8989

application-dev.properties content

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading