19

In my project I want to use environment specific property file. For example if I am running it into development it should use application.dev.properties, for production it should use application.prod.properties and so on.

I have below two files in my resources folder.

  1. application.properties (For production)
  2. application.dev.properties (For development)

I have one properties like below in each file.

For Prod

server.database.host=192.168.1.1

For Dev

server.database.host=192.168.12.125 

And I have a class like below

 public class DataSource {

     @Value(${server.database.host})
     String host;

The above code always takes prod setting (application.properties) file even though I supply proper argument for dev like --spring.profiles.active=dev

Below is the command I am using to load the dev properties file.

java -jar myjar.jar --spring.profiles.active=dev

It also prints that active profile is dev but it always connect to prod db.

alex
  • 10,900
  • 15
  • 70
  • 100
Sam
  • 2,972
  • 6
  • 34
  • 62

4 Answers4

33

A few issues I noticed:

  1. @Value property name should be a String like @Value("${server.database.host}")
  2. Profile specific property files should follow application-{profile}.properties format, e.g. application-dev.properties
  3. Try passing the profile with -D like java -Dspring.profiles.active=dev -jar app.jar
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
Kyle Anderson
  • 6,801
  • 1
  • 29
  • 41
  • 1
    Hey thanks.. I changed the properties file name as you suggested and then the command you suggested in step 3 worked well.. Thank you so much..! – Sam Jun 27 '16 at 00:07
  • 2
    This doesn't seem to work any more for SpringBoot 2.0. – markthegrea May 31 '19 at 17:17
11

Try adding a vm argument in the run configuration:

-Dspring.profiles.active=dev
abj1305
  • 635
  • 9
  • 21
6

You are supposed to specify which profile to run with as follows:

mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar

Also see documentation: https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

herrbischoff
  • 3,294
  • 1
  • 29
  • 50
hreinn
  • 185
  • 5
  • 10
2

If running on Eclipse or STS, then use following steps:

  1. Right click on project -> Run As -> Run Configuration.

  2. In the new window, select spring boot application on left side, and add details on the right and in the profile dropdown, select dev. enter image description here

  3. Click apply and run.

KayV
  • 12,987
  • 11
  • 98
  • 148