javaspring

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?


Solution

  • You can have 3 properties files: application-dev.properties, application-prod.properties, and application.properties.

    You can specify all the development properties in your application-dev.properties properties file and your production configuration properties in your application-prod.properties file

    Finally, you can specify the profile in your application.properties files as below:

    spring.profiles.active=dev
    

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