Get the current active profile in Spring Boot
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Import the annotation import org.springframework.beans.factory.annotation.Autowired; // Define a class that uses the Environment bean public class MyService { // Inject the Environment bean @Autowired private Environment environment; // Define a method that prints the active profiles public void printActiveProfiles() { // Get the array of active profiles String[] activeProfiles = environment.getActiveProfiles(); // Print each profile for (String profile : activeProfiles) { System.out.println(profile); } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Import the annotation import org.springframework.beans.factory.annotation.Value; // Define a class that uses the @Value annotation public class MyService { // Bind the value of the spring.profiles.active property to a field @Value("${spring.profiles.active}") private String activeProfiles; // Define a method that prints the active profiles public void printActiveProfiles() { // Print the field value System.out.println(activeProfiles); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Import the interface import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; // Define a configuration class that implements the interface @Configuration public class WebConfig implements WebMvcConfigurer { // Override the addCorsMappings method @Override public void addCorsMappings(CorsRegistry registry) { // Get the array of active profiles from the registry String[] activeProfiles = registry.getEnvironment().getActiveProfiles(); // Print each profile for (String profile : activeProfiles) { System.out.println(profile); } } } |
That’s all about getting the current active profile in Spring Boot.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)