Prichmp
Prichmp

Reputation: 2292

Spring use one application.properties for production and another for debug

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?

Upvotes: 42

Views: 36887

Answers (2)

diyoda_
diyoda_

Reputation: 5420

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.

Upvotes: 89

Maarten Brak
Maarten Brak

Reputation: 891

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.

Upvotes: 4

Related Questions