38

I have a Spring application and I would like to be able to switch between configurations depending if I'm debugging the server or if the server is running in production. (the difference in configurations being things like database location.)

Ideally, I'd like to pass in a command line argument to my Spring application on boot-up and set the application configuration.

I have two separate application.properties files, one with the production values, and another with the debug values. How can I switch between the two of them?

Prichmp
  • 2,112
  • 4
  • 16
  • 17
  • 2
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties and also https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files – user3707125 Jan 18 '16 at 01:46

2 Answers2

81

You can have 3 properties files, application-dev.properties, application-prod.properties and application.properties. And you can specify all the development properties in your dev properties file and production cionfiguration properties in your prod file

and specify the profile in your application.properties files as below

spring.profiles.active=dev

or you can select/override the profile using -Dprofile= argument in command line.

diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • That's what I need!. When I looked through the documentation and just got lost in @configuration classes. Nice to see that there is a simple answer. – Prichmp Jan 18 '16 at 17:44
  • Is it spring boot needed? – JaskeyLam Mar 03 '16 at 12:01
  • To run spring boot you do not need a properties file. But if there is it will check and get it from the class path. This way, you can have different configuration in different profile – diyoda_ Mar 03 '16 at 16:24
  • 2
    Is application.properties always read? For example if the profile is dev? – powder366 Mar 09 '18 at 20:00
  • 2
    @powder366 yes, it loads app.properties, then the other file, if a property is present in both, it is override by the profile – rekiem87 Jul 11 '18 at 06:06
  • 1
    Command line should be "-Dspring.profiles.active=prod" or "-Dspring.profiles.active=dev" – TdoubleG Jan 22 '19 at 06:29
  • I have 3 files application.properties, application-dev.properties, application-prod.properties. For running app I am using "java -jar Demo.jar -Dspring.profiles.active=prod", "java -jar Demo.jar -Dspring.profiles.active=dev", "java -jar Demo.jar -Dspring.profiles.active=deveoplment". But, always it is running appliciation.properties. Do I need to remove application.proerties? – Satish Patro Feb 14 '19 at 07:54
  • 1
    If I am removing "application.properties", then application fails to start – Satish Patro Feb 14 '19 at 07:55
4

Spring profiles seem the way to go. You can start your application with something like -Dprofile=. Have a look at this example.

EDIT: after re-reading your question, I came to the conclusion that you might actually want something more basic: put your database properties externally. Depending on your application you could use @Value of a property configurator. Have a look at the spring docs.

Maarten Brak
  • 816
  • 1
  • 7
  • 10