Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

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?

like image 350
Prichmp Avatar asked Jan 18 '16 01:01

Prichmp


People also ask

Can we have two application properties in spring boot?

Spring Boot supports different properties based on the Spring active profile. For example, we can keep two separate files for development and production to run the Spring Boot application.

How do I apply different application properties in spring boot?

Simply put application. properties file is a configuration file in which we put configuration key-value pairs for our spring boot application. Spring boot uses these configurations during startup to configure various properties like port no, context path, database configuration, and many other configurations.

How do I enable debug in spring boot application properties?

You can also enable a “debug” mode by starting your application with a --debug flag. You can also specify debug=true in your application. properties . When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information.

How do I set environment specific properties in spring boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.


2 Answers

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.

like image 97
diyoda_ Avatar answered Oct 09 '22 17:10

diyoda_


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.

like image 23
Maarten Brak Avatar answered Oct 09 '22 15:10

Maarten Brak