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.

pom.xml
<?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>

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

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

application.properties
#set active profile:
spring.profiles.active=DEV

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.

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 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;

@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 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;

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

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.

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

---
spring:
	profiles: PROD
property:
	myVal: Production profile activated!!

Example source code is available here.


Conclusion

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