HomeSpringSpringBootHow we use Spring Profiles using SpringBoot

How we use Spring Profiles using SpringBoot

In this article, we’re going to analyze Spring Profiles using Spring Boot and see how we can use it effectively in our project.

Introduction to Spring Profiles using Spring Boot

Spring Profiles offers a powerful and quick way to monitor the environment-based code and configuration. Using Spring Profiles, it is possible to isolate portions of our program and make it accessible only in some settings.  We need to use the @Profile annotation to restrict the availability of any @Component or @Configuration.

What is the use of @profile

The key entry point for the Spring Profile is the @Profile annotation that can be used to group items together. Take a basic example for a Database link bean where we want to make sure that a certain DB connection is only involved in DEV mode but not in output or QA/Stage mode. We will use the @Profile tag to do this.

public class DBConnectionDev implements  DatabaseService {
    public void getDBConnection() {
        System.out.println(" DB connection established for dev Env");
        System.out.println("In this post we undertand the use of Spring Profiles using SpringBoot ");


Because we have annotated DevDBConnection bean with the “development” profile, it will only be available in the Spring container if the development profile is active, in other words, if the development profile is not active, this bean will not be available/working.

The default configuration used for the Spring Profile is the same. Both beans that do not have a profile annotation are included in the default profile. You may also set the default profile in the Spring Boot by @Profile(“default”) or @Profile({“default”,””development”}).

Use of @Profile Annotation

Spring Boot offers a variety of ways to enable the profile. We may transfer profile information through the command line or use the, Spring Boot also offers a way to programmatically set the profile.

Using Command Line

We can send the profile information to Spring Boot by using the command prompt—,stage.

Using Property File

In our or application.yaml, we will use the regular Spring environment property property to define active profiles.,staging

Programmatically setting profile

You can programmatically set the active profile by calling the setAdditionalProfiles(…) method given by the SpringApplication class.

SpringApplication app = new SpringApplication(Application.class);

Please notice that the property follows the same ordering law as the other properties identified by the Spring Boot. The highest source property would overwrite every other property specified in the hierarchy. Please refer to the Spring Boot documents to see how Spring Boot reads these assets.

Profile Specific Configurations

One of the most interesting and powerful functionality offered by Spring Boot is the ability to identify and enable a profile specific file by main file.

To use profile related configuration files, we need a framework naming convention-{profile}.properties where the profile specifies the name of the intended profile. Profile-specific files will be loaded from the same place as the file, also be mindful that profile-specific properties will overwrite the properties specified in the default, regardless of whether the profile-specific files are within or outside your packed container.

To properly understand this, let’s use the same example of Database configuration where we want to specify various DB settings for Creation and Output. To do this using configuration files, we can define 2 configuration files, namely and



Understand by Example

DataBase Service

public interface DatabaseService {

    void getDBConnection();

Development Profile

public class DevDBConnection implements  DatabaseService {

    public void getDBConnection() {
        System.out.println("DEV DB connection established for onurdesk ");

Production Profile

public class ProdDBConnection implements DatabaseService {

    public void getDBConnection() {
        System.out.println("Product DB connection establish");

Spring Boot Runner

public class OnurdeskApplication implements CommandLineRunner{

    DatabaseService databaseService;

    public static void main(String[] args) {, args);

     * Callback used to run the bean.
     * @param args incoming main method arguments
     * @throws Exception on error
    public void run(String... args) throws Exception {


if you run above example with development profile as active profile, we will have the following output

DEV DB connection established

Onurdesk is a technical platform based primarily on Spring, Java, and Nodejs ecosystem app creation. We write tutorials and manuals concentrating extensively on realistic and real-life usage cases.


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

Recent Comments