Spring Boot Profiles Example

While developing applications, we may want to have different application configurations depending upon the environment we are using. We may use different environments like development environment , test environment, production environment, etc.

It is a good idea to have different configurations for different environments so that it is easy to switch between environments whenever required.

Spring boot provides an easy option to segregate different configuration related to different profiles.

For example, we may use H2 database connection properties for development environment and for production environment we may use actual database, as we may not want to mess up with production database. 😛 😉

In this article we are going to learn how to use different active profiles using spring boot application.properties and application.yml configuration file.

Version details used:

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

Spring active profile using application.properties

Create a spring boot application with required dependencies.

Let’s create a simple spring boot application with spring-boot-starter-web dependency. sample pom.xml 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>

Our auto generated spring boot application file SpringActiveProfileExampleApplication.java is given below.

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 /src/main/resources/ directory and add the following content.

#set active profile:

spring.profiles.active : This property can be used to set active profile from available spring profiles.

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

Create files with name application-DEV.properties and application-PROD.properties under the same directory( /src/main/resources/ ) and add the below given property.

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

Here, we have added a property with key property.myval in both profile configuration files with different values.

Based on the active profile configuration in application.properties configuration file, respective profile configuration will be used during application startup.

We can create profile specific configuration files with file name pattern as application-{profile}.properties under application’s class path.

Using @Profile annotation for bean configuration

Create a java file ProfileConfig.java 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 bean called myBean.

Notice the @Profile is annotation used for each bean declaration methods. This ensures which bean method to be used to configure the bean during spring application startup.

We can also use @Profile in class level along with @Component or @Configuration annotations and it will be activated based on current active profile.

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

Create a controller class SpringProfilesController.java file and add the below given 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 upon accessing http://localhost:8080/.

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

spring boot active profile

Once application is started successfully, we can verify the active profile by accessing http://localhost:8080/ .

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

#set active profile:

We should be able to get the following output.

Using application.yml file to set active profiles

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

Every profile configurations should be separated with . Our example above can be written suing application.yml file as shown below.

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

Example source code is available here.


Spring boot provides easier way to segregate different profiles, which is very useful to configure our application based on different profiles.

Hope you enjoyed the article 🙂 🙂

You may also interested in