spring-boot Create and Use of multiple application.properties files Dev and Prod environment using different datasources


Example

After succesfully setup Spring-Boot application all the configuration is handled in an application.properties file. You will find the file at src/main/resources/.

Normally there is a need to have a database behind the application. For development its good to have a setup of dev and a prod environments. Using multiple application.properties files you can tell Spring-Boot with which environment the application should start.

A good example is to configure two databases. One for dev and one for productive.

For the dev environment you can use an in-memory database like H2. Create a new file in src/main/resources/ directory named application-dev.properties. Inside the file there is the configuration of the in-memory database:

spring.datasource.url=jdbc:h2:mem:test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

For the prod environment we will connect to a "real" database for example postgreSQL. Create a new file in src/main/resources/ directory named application-prod.properties. Inside the file there is the configuration of the postgreSQL database:

spring.datasource.url= jdbc:postgresql://localhost:5432/yourDB
spring.datasource.username=postgres
spring.datasource.password=secret

In your default application.properties file you are now able to set which profile is activated and used by Spring-Boot. Just set one attribute inside:

spring.profiles.active=dev

or

spring.profiles.active=prod

Important is that the part after - in application-dev.properties is the identifier of the file.

Now you are able to start Spring-Boot application in develop or production mode by just changing the identifier. An in-Memory database will startup or the connection to a "real" database. Sure there are also much more use cases to have multiple property files.