119

In my Spring Boot application, i want to create environment specific properties file. The packaging type of my application in war and i am executing it in embedded tomcat. I use sts and execute the main from sts itself.

  1. Can i have environment specific properties file like application-${env-value}.properties?

In above case, env-value will have values as local/devl/test/prod

  1. Where to set the env-value file? For local, i can set it as the jvm argument through sts

  2. Who reads the application.properties in Spring Boot application.

  3. How to load the environment specific properties file? For ex - if i set the database uid,pwd, schema etc in environment specific property file, in that case will the datasource be able to understand the properties in it?

  4. Can i use application.properties and application-local.properties file at same time?

user3534483
  • 2,133
  • 5
  • 22
  • 19
  • Have you read [this](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html)? It basically is supported out-of-the-box. – M. Deinum Aug 25 '15 at 06:03
  • I did but that is not working. So i have application.properties file and application-local.properties file at same location. application-local.properties file contain db related properties. application.properties has a single property in it regarding spring mvc . Also i added -Dprofile=local as debug configuration but this value is not getting picked up – user3534483 Aug 25 '15 at 06:25
  • because you need to set `spring.active.profiles` so use `-Dspring.active.profiles=local`. instead. – M. Deinum Aug 25 '15 at 06:26
  • No Success sir. so instead of -Dprofile, i am now using -Dspring.active.profiles=local. And then in my configuration file i try to fetch the value String driverClassName = env.getProperty("driverClassName"); env is the Environment from spring – user3534483 Aug 25 '15 at 06:36
  • -Dspring.active.profiles=local i am setting as debug configuration – user3534483 Aug 25 '15 at 06:37
  • Worked. The property is "spring.profiles.active" – user3534483 Aug 25 '15 at 06:46
  • 1
    Sorry my bad, mixed it around :). – M. Deinum Aug 25 '15 at 06:49
  • Half Information on this page - http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html and other half on this page - http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties – user3534483 Aug 25 '15 at 06:54
  • @user3534483 , kindly help me how you did it? where did you set spring.profiles.active ? – Ashish Burnwal Dec 14 '17 at 11:53

4 Answers4

222

Spring Boot already has support for profile based properties.

Simply add an application-[profile].properties file and specify the profiles to use using the spring.profiles.active property.

-Dspring.profiles.active=local

This will load the application.properties and the application-local.properties with the latter overriding properties from the first.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 2
    Can we override the use of application properties with another file for specific profile without loading both of them ? For tests or dev for example ? – Hassam Abdelillah Apr 19 '16 at 13:40
  • 2
    M. Deinum, I too have similar issue, I have application.yml and application-qa.yml and I gave -Dspring.profiles.active=qa, both files are getting read but properties from application-qa.yml are not overwriting the properties from application.yml, I am always seeing properties from application.yml only ? is there any way to specify the priority ? – Suresh Dec 08 '16 at 15:59
  • 6
    Hi Both, This can solve your problems. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties Example: https://www.mkyong.com/spring-boot/spring-boot-profile-based-properties-and-yaml-example/ – Anand Varkey Philips Aug 14 '17 at 12:49
  • I am writing code for aws lambda and can pass this key=value as environment value - which can be accessed using System.getenv() - will it work ? – Abdeali Chandanwala Nov 18 '17 at 09:47
  • I have 4 application-{profile}.properties file. And I want to run the same jar file in different environment, let us say dev, test, prod. So i cant use -Dspring.profiles.active=local. Kindly correct me if I am missing something. – Ashish Burnwal Dec 14 '17 at 11:28
  • And why wouldn't that be possible... The whole point of using the `-Dspring.profiles.active=` is that you CAN run it in multiple environments. – M. Deinum Dec 14 '17 at 11:36
  • Do you know, if one key is not found in application-dev.prop file, in that case will it search in application.properties?? – Jaikrat Dec 03 '18 at 10:46
  • If we use "-Dspring.profiles.active=local" at beginning just after java it works... it should be before application.jar – Cpp crusaders Apr 21 '22 at 19:23
  • --spring.profiles.active=local to run with java -jar command. ex: java -jar myapp.jar --spring.profiles.active=dev – cherish sham May 17 '22 at 07:26
29

Yes you can. Since you are using spring, check out @PropertySource anotation.

Anotate your configuration with

@PropertySource("application-${spring.profiles.active}.properties")

You can call it what ever you like, and add inn multiple property files if you like too. Can be nice if you have more sets and/or defaults that belongs to all environments (can be written with @PropertySource{...,...,...} as well).

@PropertySources({
  @PropertySource("application-${spring.profiles.active}.properties"),
  @PropertySource("my-special-${spring.profiles.active}.properties"),
  @PropertySource("overridden.properties")})

Then you can start the application with environment

-Dspring.active.profiles=test

In this example, name will be replaced with application-test-properties and so on.

Denis Zavedeev
  • 7,627
  • 4
  • 32
  • 53
Tzen
  • 1,376
  • 11
  • 11
  • 5
    Problem is that `$spring.profiles.active}` is an array/comma seperated string. So it might not do what you expect/want it to do when there are multiple profiles active. – M. Deinum Dec 08 '16 at 18:52
  • That is true, but works fine for two profiles - one default and one defined with PropertySource. For more advanced property handling, I think you will have to look outside Spring. Also PropertySource is only read during startup. Other, more advanced libraries for property handling, can change properties run-time as well. – Tzen Dec 09 '16 at 07:40
  • 3
    This worked, but I still don't understand why I had to do this manually. In one of my other repository I didn't had to do this. – The Java Guy Oct 25 '17 at 02:03
9

we can do like this:

in application.yml:

spring:
  profiles:
    active: test //modify here to switch between environments
    include:  application-${spring.profiles.active}.yml

in application-test.yml:

server:
  port: 5000

and in application-local.yml:

server:
  address: 0.0.0.0
  port: 8080

then spring boot will start our app as we wish to.

Cuttlefish
  • 99
  • 2
  • 4
0

My Point , IN this arent way asking developer to create all environment related in single go, resulting in risk of exposing Production Configuration to end developer

as per 12-Factor, shouldnt be enviornment specific reside in Enviornment only .

How do we do for CI CD

  • Build Spring one time and promote to aother environment, in that case, if we have spring jar has all environment, it iwll security risk, having all environment variable in GIT