Spring Boot @Profile Example

1. Introduction

The @Profile annotation in Spring Boot is used to conditionally enable components based on the active profiles. It's an essential feature for separating parts of the application configuration that should be different across environments, such as development, testing, and production.

Key Points:

1. @Profile allows for conditional bean registration based on the specified profiles.

2. It supports notations for single or multiple profiles, including the use of ! for negation.

3. Profiles can be activated programmatically, via application properties, or as runtime parameters.

2. Implementation Steps

1. Create a Spring Boot application.

2. Define configuration classes or components with @Profile to designate when they should be active.

3. Activate profiles using application properties or command-line arguments.

4. Run the application with different profiles to see conditional configurations.

3. Implementation Example

Here is the complete code that demonstrates the usage of @Profile annotation:
// Define a configuration that only applies to the "dev" profile
@Configuration
@Profile("dev")
public class DevConfig {
    @Bean
    public String devBean() {
        return "Development Bean";
    }
}

// Define a configuration that only applies to the "prod" profile
@Configuration
@Profile("prod")
public class ProdConfig {
    @Bean
    public String prodBean() {
        return "Production Bean";
    }
}

// Main application class
@SpringBootApplication
public class ProfileExampleApplication {
    public static void main(String[] args) {
        // Step 4: Run the application with a specific profile
        SpringApplication app = new SpringApplication(ProfileExampleApplication.class);
        app.setAdditionalProfiles("dev"); // Activate 'dev' profile
        ConfigurableApplicationContext ctx = app.run(args);

        // Fetch beans and print out their presence
        String devBean = ctx.getBean("devBean", String.class);
        System.out.println(devBean);

        // Attempt to fetch 'prodBean', should throw an exception since 'prod' profile is not active
        try {
            String prodBean = ctx.getBean("prodBean", String.class);
            System.out.println(prodBean);
        } catch (NoSuchBeanDefinitionException e) {
            System.out.println("Production bean is not available under 'dev' profile");
        }
    }
}

// You can run the application with the 'dev' profile active using command-line argument:
// java -jar myapp.jar --spring.profiles.active=dev

// Or using application.properties:
// spring.profiles.active=dev

Output:

Development Bean
Production bean is not available under 'dev' profile

Explanation:

1. DevConfig and ProdConfig are configuration classes marked with @Configuration. Each class has a @Profile annotation indicating the profile under which its beans should be registered.

2. devBean is a bean method in DevConfig, which will only be registered when the 'dev' profile is active.

3. prodBean is a bean method in ProdConfig, only registered when the 'prod' profile is active.

4. In the ProfileExampleApplication class, the application is started with the 'dev' profile activated, which causes only devBean to be available in the application context.

5. The output confirms that devBean is present, while prodBean is not, due to the active profiles.

6. This demonstrates how @Profile can be used to conditionally configure beans for different environments.


Comments