1.

What are Spring Profiles? How do you implement it using Spring Boot?

Answer»

A profile is a feature of Spring framework that allows us to MAP the beans and components to certain PROFILES. A profile can be assumed to be a group or an environment like dev, test, prod, etc.; that needs a certain kind of behavior and/or requires to maintain distinct functionalities across the profiles. So, when the application is running with ‘dev’ (Development) profile only certain beans can be loaded and when in ‘prod’ (Production) certain other beans can be loaded.

In Spring Boot we use @Profile annotation to map bean to a PARTICULAR profile by taking the names of one (or multiple) profiles.

Let’s say we have a COMPONENT class that is used to record and mock the REST requests and responses. However, we want to activate this component only in dev profile and disable in all other profiles. We annotate the bean with “dev” profile so that it will only be present in the container during development.

@Component @Profile("dev") public class DevMockUtility

Profiles are activated using application.yml in the Spring project:

spring.profiles.active=dev

To set profiles programmatically, we can also use the SpringApplication class:

SpringApplication.setAdditionalProfiles("dev");


Discussion

No Comment Found