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

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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.asb.example</groupId>
	<artifactId>spring-active-profile-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-active-profile-example</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Below is the spring boot application class.

package com.asb.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@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:
spring.profiles.active=DEV

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.

application-DEV.properties

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

application-PROD.properties

#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;
@Configuration
public class ProfileConfig {
	@Profile("DEV")
	@Bean(name = "myBean")
	public String createDevBean() {
		return "My bean is configured with Development configurations!!";
	}
	@Profile("PROD")
	@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;
@RestController
public class SpringProfilesController {
	@Value("${property.myval}")
	private String profile;
	@Autowired
	private String myBean;
	@GetMapping("/")
	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:
spring.profiles.active=PROD

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.

spring:
	profiles: 
          active: DEV
---
spring:
	profiles: DEV
property:
	myVal: Development profile activated!!
---
spring:
	profiles: PROD
property:
	myVal: Production profile activated!!

Conclusion

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.