0

I want to pick up the environment specific properties file at run time from class path

I am still learning Spring Boot, we can do it using environment variables or vm arguments or put in application.properties in maven run configuration

However we will always have to change the code at the time of deployment for a specific env if we put it application.properties.

I want to pick it up at run time without modifying the code at all.

3
  • add SPRING_ACTIVE_PROFILES=dev as environment variable Commented Jul 31, 2019 at 5:48
  • yeah definitely, i don't want to change it every time i deploy to different environments. Commented Jul 31, 2019 at 5:52
  • In different environments you will set it diff value right? Commented Jul 31, 2019 at 5:55

2 Answers 2

4

Use spring profiles. Create application.properties for each profile like below:

application-dev.properties application-prod.properties 

Keep the application.properties as the master copy. If a property is missing in the profile-specific file then it will be picked from the master application.properties file as a fallback.

Then either set the following in application.properties or set as an environment variable or as a system property to activate a profile:

In application.properties:

spring.profiles.active=dev 

Environment variable:

export spring_profiles_active=dev 

System Property:

java -jar -Dspring.profiles.active=dev xyzapp.jar 
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you can try System.setProperty() to set something at application level. But if you don't reset it will remain throughout application.

2 Comments

How can I do that?
System.getProperty(key) will be used in java to fetch something set at application level and System.SetProperty(key,value) will be used in java to set something in application. Here key is the property u want to fetch or update.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.