Spring Boot Profiles Tutorial

In this tutorial, you will learn how to use Spring Boot Profiles.

You can use Spring Boot Profile functionality when providing application configuration for a specific environment. For example, when your application runs in a development environment, then your application will use a specific for that environment application.properties file. And when you are ready to move your application to a production environment, it will use a different application.properties file specific to the production environment.

Additionally to using a specific environment properties file, you can also use Spring Boot Profiles to configure your application to access specific environment Java Beans. Let’s learn how to do it.

Create Profile Specific Properties File

Spring Boot Profiles help our application use a specific properties file depending on which profile is set to Active. When we create a new Spring Boot Project, the application.properties file is created for us, and we can use it for one profile(Developer profile). Let’s create a new application properties file that will be used for another profile, such as a production profile.

Application Properties File for a Specific Profile

Let’s create a new application properties file for the Production profile. When creating an application properties file used for a specific profile, add the profile name at the end of the file name. Like so:

application-production.properties

where

production – is the name of the profile.

Below is an example of the application-production.properties file, which I will use in this tutorial for the production profile.

spring.profiles.active = production

myAPI.messages.welcome = "You are working with Production profile"


database.name = photo_app_production
database.url = jdbc:mysql://localhost:3306/${developer.database.name}
database.username = "production"
database.password = "1fhFy4Yr73H4Y9f"

Now that you know how to create a Profile-specific application properties file. You can create more property files each for a specific Profile you need. For example:

application-preProduction.properties.

You can create multiple property files in your application. Each property file will be used by a specific Spring Boot Profile. Which profiles and property files to create is up to you and will depend on the needs of your application. For example:

  • application.properties – Base configuration. If a specific Profile is used, then properties in a specific profile will override properties in a base configuration file.
  • application-qa.properties – For a qa profile,
  • application-preProduction.properties – For a preProduction profile,
  • application-production.properties – For a production profile.

Start up Spring Boot Application for a Specific Profile

Let’s assume we have the following two application properties files in our Spring Boot App:

  • application.properties – For a default or a developer profile,
  • application-production.properties – For production profile.

To start the Spring Boot application for a production profile, you will use the following maven command:

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=production

To start a Spring Boot application that will use a default application.properties file only, do not specify any profile details and simply use the following maven command:

mvn spring-boot:run

I hope this tutorial was helpful to you. If you are interested in learning more about Spring Boot and enjoy learning by following a step-by-step series of video lessons, check the below list of video courses.


Leave a Reply

Your email address will not be published. Required fields are marked *