Springboot not loading application.dev.properties file

Sam picture Sam · Jun 26, 2016 · Viewed 20.1k times · Source

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.

Answer

Kyle Anderson picture Kyle Anderson · Jun 26, 2016

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