Profiles in Spring Boot - Developers must Know!

Profiles in Spring Boot - Developers must Know!

This article is helping you to explore the simple but elegant concept is Profiles for spring boot developers. If you are a developer you definitely come across scenarios like maintaining the different configurations for dev, QA, SIT, UAT, and PROD environments. The number of environments may differ but at least two or two plus environments most the organizations have.

Let us consider one more scenario where you want to enable or disable a few things based on the user having a free or paid profile. Spring Boot provides out-of-scope support to handle this kind of scenario.

Spring Boot provides profiles that help to create environment-specific configurations and stereotypes in the applications. Any @Component, @Configuration or @ConfigurationProperties can be marked with @Profile to limit when it is loaded.

public class DummyProductsComponent {      
    // implementation 

In the above snippet, you mentioned that this component is only for the dev profile. If you want to mention all other profiles apart from the dev profile then use an exclamation ( ! ) mark before dev like this @Profile("!dev")

If you want to learn more about the Spring Cloud Config server hit the below links:

  1. Configuration As a Service using Spring Cloud Config
  1. Spring Cloud Config: Configuring Random Values in Properties

Set Profile:

Setting up the will specify the active profile of the running instance. This property can be set via different methods using the, command-line, or environment variables.

While running jar using the command line use the below command to set the profile

java -jar profieldemoapplication.jar


java -jar profieldemoapplication.jar

While setting in environment variables.

export spring_profiles_active=dev

Above all ways are non-programatic ways or non-code changes. You can even set a profile using code changes or programmatically in two ways, the first one using ConfigurableEnvironment class and another one using SpringApplication.

private ConfigurableEnvironment env; 

#some code



SpringApplication app = new SpringApplication(Main.class);

Till now you just now you can activate or set one single profile as an active profile but there are situations where you want to set multiple profiles as an active. Don't worry spring boot provides support from the 2.4 version onwards that's known as the profile group.[0]=devsqldb[1]=devnosqldb


In this article, you learn a very simple but important concept like spring boot profile. This helps to reduce your effort by hours. You can set the profile in a different way like CLI, configurations, and programmatically even you can set one single profile or more than one. Let me know in the comments if you have any interesting use-case for profiles.

More such articles:


If this article adds any value for you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!