This post will discuss how to get the current active profile in Spring Boot.

There are several ways to get the current active profile in Spring Boot, which is a way to specify different configurations or behaviors for different environments, such as development, testing, or production. Here are some of the possible methods:

1. Using @Autowired Annotation

Using the @Autowired annotation to inject the Environment bean into a class, and then using the getActiveProfiles() method to return an array of strings that represent the active profiles.

2. Using @Value Annotation

Using the @Value annotation to bind the value of the spring.profiles.active property to a field or a parameter, and then using the field or the parameter to access the active profiles. The spring.profiles.active property can be set in various ways, such as in the application.properties file, as a command-line argument, or as an environment variable.

3. Using WebMvcConfigurer Interface

Using the WebMvcConfigurer interface to implement a configuration class that overrides the addCorsMappings() method, and then using the CorsRegistry parameter to access the active profiles. The WebMvcConfigurer interface provides various methods to customize the web application configuration, such as enabling CORS (Cross-Origin Resource Sharing) for specific URL patterns.

That’s all about getting the current active profile in Spring Boot.