Spring Boot Profiles Tutorial

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

You can use Spring Boot Profile functionality when you need to provide 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 which is specific to production environment. Additionally to using a specific to an environment properties file, you can also use Spring Boot Profiles to configure your application to access specific to an 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 which will be used for another profile which will be for example a production profile.

Application Properties File for a Specific Profile

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

application-production.properties

where

production – is a name of profile.

Below is an example of application-production.properties file which I am going to use in this tutorial for 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 when 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 really up 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 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 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 for you. If you are interested to learn more about Spring Boot and you enjoy learning by following a step by step series of video lessons, then check the below list of video courses.