Profiles in Spring Boot - Developers must Know!

Profiles in Spring Boot - Developers must Know!

Maheshwar Ligade's photo
Maheshwar Ligade
·May 24, 2022·

3 min read

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.

@Component
@Profile("dev") 
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 spring.profiles.active will specify the active profile of the running instance. This property can be set via different methods using the application.properties, command-line, or environment variables.

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

java -jar -Dspring.profiles.active=dev profieldemoapplication.jar

OR

java -jar profieldemoapplication.jar --spring.profiles.active=dev

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.

@Autowired 
private ConfigurableEnvironment env; 

#some code

env.setActiveProfiles("dev");

OR

SpringApplication app = new SpringApplication(Main.class);
app.setAdditionalProfiles("dev");

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.

spring.profiles.group.development[0]=devsqldb 
spring.profiles.group.development[1]=devnosqldb

Conclusion:

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:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

==========================**=========================

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!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this