Spring Boot Features + Active Profile

Spring Boot is not an entirely new framework, in fact, it’s an extension of the Spring framework. Spring Boot includes all of the same modules that Spring does.

Features of Spring Boot

Spring Boot does this by automatically pre-configuring virtually all of the third party libraries that you will use.

Spring Boot also makes use of the Parent pom.xml. Instead of explicitly specifying all the dependencies and versions that you need within your pom.xml, in your Maven build configuration, you’ll simply use a parent pom which has most of the dependencies pre-configured with the right versions as well.

The starter dependency templates are an important part of Spring Boot.

The auto-configuration feature in Spring Boot relies on specific Spring annotations, which are variants of the @Configuration annotation. Each @Configuration annotation is an opinionated default specified by your app. Now, these @Configuration annotations are used conditionally, which means that if you have overridden the default specification for some property, your overridden version will be used, not the default. Thus, Spring Boot respects user-provided defaults. So if you don’t want the default specification, you can override it. Otherwise, the default specification is there for you to use. Spring Boot auto-configuration automatically detects and adds beans that you haven’t explicitly manually configured. Within a Spring Boot app, you need to opt-in to this auto-configuration by adding to the entry point of your app.

@EnableAutoConfiguration or @SpringBootApplication

And finally, Spring Boot also simplifies Spring by allowing you to use the Spring Initializr. The Spring Initializr is basically just a web interface that you can use to set up your initial project structure for Spring Boot.

Instead of manually setting up your Spring Boot project, you can use the Spring Initializr and download a zip file with your project. And all of the dependencies that you’ve specified set up for you automatically.

Spring Boot Profiles – How to Set an Active profile?

Once you have profile specific configuration, you would need to set the active profile in an environment.

Using -Dspring.profiles.active=qa in VM Arguments
Use spring.profiles.active=qa in application.properties

Lets set it in application.properties by add another property to application.properties
spring.profiles.active=qa

Example: Features of Spring Boot

In this example, we will see how we can set an active profile through properties file or runtime.

Spring Boot Project Structure

High-level project structure would be like this.

Spring Boot Project Structure

Maven File (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>

	<groupId>com.example</groupId>
	<artifactId>spring-boot-active-profile</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-active-profile</name>
	<description>Microservice for setting an active profile in Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>		
		<version>2.4.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
    
    <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
  
</project>

Application Config (application.properties, application-dev.properties, application-qa.properties)

application.properties

spring.profiles.active=dev

application-dev.properties

file.directory=some_path_here
cmdb.resource-url=http://www.dev.com
cmdb.resourcePort[0]=80
cmdb.resourcePort[1]=443

application-qa.properties

file.directory=some_path_here
cmdb.resource-url=http://www.qa.com
cmdb.resourcePort[0]=8080
cmdb.resourcePort[1]=543

Spring Boot Main: @SpringBootApplication

package com.the.basic.tech.info.active.profile.app;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

/*
 mvn spring-boot:run -Dspring.profiles.active=qa
 NOTE: Give above command from command line
 replace qa with dev
 */
@Configuration
@ConfigurationProperties("cmdb")
class CmdbProperties {

	private String resourceUrl;
	private List<Integer> resourcePort;

	@Override
	public String toString() {

		return "resourceUrl: " + this.resourceUrl + "\n" + "resourcePort: " + this.resourcePort + "\n";
	}

	public String getResourceUrl() {
		return resourceUrl;
	}

	public void setResourceUrl(String resourceUrl) {
		this.resourceUrl = resourceUrl;
	}

	public List<Integer> getResourcePort() {
		return resourcePort;
	}

	public void setResourcePort(List<Integer> resourcePort) {
		this.resourcePort = resourcePort;
	}
}

@SpringBootApplication
public class SpringBootActiveProfileExample {
	@PostConstruct
	public void print() {
		System.out.println("Properties from properties file:" + cmdbProperties.toString());
	}

	@Autowired
	private CmdbProperties cmdbProperties;

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(SpringBootActiveProfileExample.class, args);
	}
}

Set dev profile: mvn spring-boot:run

spring.profiles.active=dev

Application logs

2021-05-29 17:41:18.230  INFO 11412 --- [           main] t.i.a.p.a.SpringBootActiveProfileExample : The following profiles are active: dev
2021-05-29 17:41:19.750  INFO 11412 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-05-29 17:41:19.750  INFO 11412 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-05-29 17:41:19.750  INFO 11412 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-05-29 17:41:19.850  INFO 11412 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-05-29 17:41:19.850  INFO 11412 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1551 ms
Properties from properties file:resourceUrl: http://www.dev.com
resourcePort: [80, 443]

2021-05-29 17:41:20.019  INFO 11412 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-29 17:41:20.166  INFO 11412 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-29 17:41:20.182  INFO 11412 --- [           main] t.i.a.p.a.SpringBootActiveProfileExample : Started SpringBootActiveProfileExample in 2.5 seconds (JVM running for 3.106)

Set qa profile: mvn spring-boot:run

spring.profiles.active=qa
2021-05-29 17:43:50.651  INFO 3212 --- [           main] t.i.a.p.a.SpringBootActiveProfileExample : The following profiles are active: qa
2021-05-29 17:43:52.811  INFO 3212 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-05-29 17:43:52.831  INFO 3212 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-05-29 17:43:52.831  INFO 3212 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-05-29 17:43:52.927  INFO 3212 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-05-29 17:43:52.927  INFO 3212 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2213 ms
Properties from properties file:resourceUrl: http://www.qa.com
resourcePort: [8080, 543]

2021-05-29 17:43:53.112  INFO 3212 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-29 17:43:53.281  INFO 3212 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-29 17:43:53.297  INFO 3212 --- [           main] t.i.a.p.a.SpringBootActiveProfileExample : Started SpringBootActiveProfileExample in 3.178 seconds (JVM running for 3.697)

Override profile @runtime : mvn spring-boot:run -Dspring.profiles.active=qa

mvn spring-boot:run -Dspring.profiles.active=qa
2021-05-29 17:49:53.519  INFO 9416 --- [           main] t.i.a.p.a.SpringBootActiveProfileExample : The following profiles are active: qa
2021-05-29 17:49:55.277  INFO 9416 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-05-29 17:49:55.293  INFO 9416 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-05-29 17:49:55.293  INFO 9416 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-05-29 17:49:55.393  INFO 9416 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-05-29 17:49:55.393  INFO 9416 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1789 ms
Properties from properties file:resourceUrl: http://www.qa.com
resourcePort: [8080, 543]

2021-05-29 17:49:55.594  INFO 9416 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-05-29 17:49:55.741  INFO 9416 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-29 17:49:55.748  INFO 9416 --- [           main] t.i.a.p.a.SpringBootActiveProfileExample : Started SpringBootActiveProfileExample in 2.89 seconds (JVM running for 3.397)

Download Source Code (Attached)