15

I have a Spring Boot application that will run in various environments, and based on the environment it runs in, it will connect to a different database. I have a few application.properties files, one for each environment which look like such:

application-local.properties:

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789

application-someserver.properties:

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass

etc. etc.

On each of my environments, I have a environment variable called MYENV which is set to the type of environment it is, for example local or someserver (the name of the application-{env}.properties files perfectly matches the environment name).

How can I get spring boot to read this environment variable and automatically choose the correct .properties file? I don't want to have to do the whole -Dspring.profiles.active=someserver because of the way this package is deployed (it will not be running as a jar).

Tanishq dubey
  • 1,522
  • 7
  • 19
  • 42
  • Possible duplicate of [Using env variable in Spring Boot's application.properties](https://stackoverflow.com/questions/35531661/using-env-variable-in-spring-boots-application-properties) – Alex R Oct 11 '18 at 02:48

3 Answers3

17

How can I get spring boot to read this environment variable and automatically choose the correct .properties file?

Tell Spring Boot to select the Active Profile from the MYENV property or environment variable. One way of doing this is to add the following into your application.properties:

spring.profiles.active=${MYENV}

This way Spring Boot will set spring.profiles.active to the value of MYENV environment variable.

I don't to have to do the whole -Dspring.profiles.active=someserver because of the way this package is deployed (it will not be running as a jar)

You can use the corresponding environment variable to that spring.profiles.active, if you don't like to pass a system property through -D arguments. That corresponding environment variable is SPRING_PROFILES_ACTIVE. For example if you set the SPRING_PROFILES_ACTIVE to local, the local profile will be activated. If you're insisting to use MYENV environment variable, the first solution is the way to go.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • 2
    Is it possible to have a "default" configuration so that in the env variable does not match any files, a standard is chosen? – Tanishq dubey Jul 12 '16 at 20:43
  • Also, put default and common configurations into `application.properties`. It's always will be used but with a lower precedence with respect to profile specific configurations – Ali Dehghani Jul 13 '16 at 03:48
  • @AliDehghani I set values in my system variable but application.properties is not picking it up. Variable name: user Value: pass. In my app.properties spring.datasource.username= ${user} – JayC Feb 23 '17 at 20:45
  • Is this possible in Spring context and not in Spring Boot...also do I need to keep the property in application.properties file and then using the active profile over ride the property? - or its if I dont put ebvironment based properties in application.properties and just in its [profile].properties file – Abdeali Chandanwala Nov 22 '17 at 14:37
  • I want minimal dependencies - as I am coding for AWS Lambda - Thanx – Abdeali Chandanwala Nov 22 '17 at 14:40
1

If you are deploying that application to some container (tomcat, weblogic, ...) you can specify environment variable. For example lets specify environment variable application1.spring.profiles.active=someserver and then in your application.properties set property spring.profiles.active=${application1.spring.profiles.active}

bilak
  • 4,526
  • 3
  • 35
  • 75
0

Using Spring context 5.0 I have successfully achieved loading correct property file based on system environment via the following annotation

@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:application-${MYENV:test}.properties")})

Here MYENV value is read from system environment and if system environment is not present then default test environment property file will be loaded, if I give a wrong MYENV value - it will fail to start the application.

Note: for each profile, you want to maintain you need to make an application-[profile].property file and although I used Spring context 5.0 & not Spring boot - I believe this will also work on Spring 4.1

This is the best solution I believe for my AWS Lambda - with minimal dependencies

Abdeali Chandanwala
  • 8,449
  • 6
  • 31
  • 45