1. What is application.properties file?

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.

2. Why do we need multiple application.properties files?

Suppose you are working on a live project which has a huge database with thousands of records. Obviously, you don't want to mess with the records in the database. So you can create two applications.properties files one for the original database and the other for the test database which you use during development.

3. Creating application.properties file

Spring boot automatically generates an application.properties file for you while generating a spring project. If due to any reasons application.properties file is not there you can create it in the src/main/resources directory. Just right click on the resources folder and select create a file and name it application.properties file.

image.png

4. Creating multiple application.properties file

Now to create application.properties file for working with the test database during development just create a new file under the resources folder and name it application-dev.properties. This file will contain all the configuration that you need for development. Now create another application.properites file in the same directory and name it application-prod.properties. This is for production configuration.

image.png

Note: keep the -dev and -prod parts in mind while naming the files. Spring will use these parts to distinguish both the files.

5. Adding configurations

5.1. application-dev.properties

Now add the configurations for the database that you want to use during development to this file.

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/testDB
spring.datasource.username=testUser
spring.datasource.password=password

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.properties.hibernate.use_sql_comments = true

5.2. application-prod.properties

Add the configurations for the original database to this file. This will be used in production.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/originalDB
spring.datasource.username=originalUser
spring.datasource.password=secret

6. Switching between development and production

Now to use the application-dev.properties during development just add the following line to the default application.properties file.

spring.profiles.active=dev

And for production purposes change the line to

spring.profiles.active=prod

This will tell the spring boot which configuration file it needs to use to configure the properties of your application during startup.

7. Conclusion

Now we know why we use application.properties and how to use multiple application.properties. We can create any number of application.properties file according to our needs and use them in our spring boot application.

Also Read:

Creating Rest API in Spring Boot

Using Hibernate with Spring Boot

Using Lombok in Java and Spring Boot