Spring Boot Profiles Example

While building applications, we may want to have different application configurations. This may also depend on the environment(like development, test, production, etc) we are using.

It is a good idea to have different configurations for each environment. This makes it is easy to switch between profiles whenever required.

The Spring boot provides an easy option to divide different configurations related to multiple profiles.

For example, we may use H2 database connection properties for the development profile. Now, for the production profile, we may use the actual database. We can have two different profiles to separate these profiles.

In this post, we will learn how to use different active profiles using the spring boot.

Version details:

  • Spring Boot version 2.1.6.RELEASE
  • JDK version 1.8
  • STS 4

Table of Contents

Using the properties file to set the active profile

Create a spring boot application with the required dependencies. Add the web dependency to the application’s pom.xml file.

The complete pom file is given below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>

Below is the spring boot application class.

package com.asb.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringActiveProfileExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringActiveProfileExampleApplication.class, args);

Open the application.properties file under the /src/main/resources/ folder and add the below content.

#set active profile:

We can use the spring.profiles.active property to set active profiles from available spring profiles.

We have set the active profile value as DEV, in the above configuration file to activate the development profile.

Create two configuration files under the same folder, for each profile. Add the property shown below.


#DEV profile configurations:
property.myval=Development profile activated!!


#PROD profile configurations:
property.myval=Production profile activated!!

Here, we have added a simple property in both of the profile configuration files with different values.

Spring boot uses the configuration file based on the active profile during application startup.

We can also create a profile specific configuration files with file name pattern application-{profile}.properties under the application’s classpath.

Using the @Profile annotation for bean configuration

Create a java file with the name and add the content given below.

package com.asb.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

public class ProfileConfig {

    @Bean(name = "myBean")
    public String createDevBean() {
        return "My bean is configured with Development configurations!!";

    @Bean(name = "myBean")
    public String createProdBean() {
        return "My bean is configured with Production configurations!!";

Here, we have created a spring configuration class, which configures a bean called myBean.

We can use the @Profile annotation with each bean declaration method to ensure which bean method the application context should use to configure the bean during spring application startup.

In addition, we can also use @Profile at the class level. We can use this along with the @Component or @Configuration annotations. Spring activates the correct bean based on the active profile.

We can use the @Profile annotation at the class level with @Profile({“p1”, “p2”}), to enable a configuration class when two profiles are active(p1 and p2).

Create a controller class with the name SpringProfilesController, and add the below content.

package com.asb.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class SpringProfilesController {

    private String profile;

    private String myBean;

    public String getActiveProfile() {
        return "Active profile is :: " + profile + " <br/> <br/>Configured bean value is :: " + myBean;

Here, We have a Rest controller, which prints active profile and configured bean value. We can access our application at http://localhost:8080/.

Start the spring boot application. We should be able to see the DEV profile activation on the application startup as shown below.

spring boot active profile

Once the application starts successfully, we can verify the active profile as shown below.

Change the properties file by setting the active profile value to PROD.

#set active profile:

We should be able to get the below output.

Using yml file to set the active profile

If we are using an application.yml configuration file, we can use a single property file to define all the profiles.

Every profile configuration should be separated with — characters.

Our example above can be written using the yml file as shown below.

    active: DEV
  profiles: DEV
  myVal: Development profile activated!!
  profiles: PROD
  myVal: Production profile activated!!


Spring boot provides an easier way to group different profiles. This is also an useful to configure our application based on different profiles.

The example source code is available here.