Spring Boot @ConditionalOnProperty Example

1. Introduction

Spring Boot provides a powerful suite of features designed to work with the configuration properties in your application. One such feature is the @ConditionalOnProperty annotation. This annotation allows developers to conditionally enable or disable certain beans based on the presence and value of a configuration property. This is particularly useful for feature flagging or allowing for easy toggling of features during development or in different environments. In this blog post, we will demonstrate how to use @ConditionalOnProperty in a Spring Boot application.

Key Points:

1. @ConditionalOnProperty allows beans to be conditionally registered based on the presence and value of a configuration property.

2. This annotation supports fine-grained control over your beans and their initialization in the application context.

3. It can be particularly useful for activating or deactivating features or configuration in different environments, like development, staging, or production.

4. The properties can be set in various ways, including application.properties, application.yml, or environment variables.

5. @ConditionalOnProperty can check for the existence of a property, the absence of a property, or the specific value of a property.

2. Implementation Steps

1. Define a bean that you want to conditionally enable.

2. Annotate the bean method or class with @ConditionalOnProperty.

3. Configure the properties in application.properties or another configuration source.

4. Write a main application class to run the Spring Boot application.

5. Observe the bean's behavior based on the presence or value of the configured property.

3. Implementation Example

// Step 1: Define a service that you want to conditionally enable
class FeatureService {
    public String featureStatus() {
        return "Feature is enabled";
    }
}

// Step 2: Define a configuration that creates the service bean conditionally
@Configuration
public class FeatureConfig {

    // This bean will only be created if the 'feature.enabled' property is set to true
    @Bean
    @ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
    public FeatureService featureService() {
        return new FeatureService();
    }
}

// Step 3: Set the property in application.properties (located in src/main/resources/)
/*
feature.enabled=true
*/

// Step 4: Create the main application class
@SpringBootApplication
public class ConditionalOnPropertyApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ConditionalOnPropertyApplication.class, args);

        // Step 5: Access the FeatureService bean and print out its status
        FeatureService featureService = context.getBean(FeatureService.class);
        System.out.println(featureService.featureStatus());
    }
}

Output:

Feature is enabled

Explanation:

1. FeatureService: A simple service class that provides functionality depending on a feature toggle.

2. @ConditionalOnProperty: This annotation is added to the featureService bean method, making the registration of this bean conditional based on the feature.enabled property.

3. name = "feature.enabled": The name of the property that @ConditionalOnProperty checks.

4. havingValue = "true": The value that the property must have for the bean to be registered.

5. FeatureConfig: A configuration class that declares the conditional bean based on the property.

6. @Configuration: Marks the class as a source of bean definitions.

7. @SpringBootApplication: Marks the application class and triggers auto-configuration, component scanning, and configuration processing.

8. application.properties: The file where the feature.enabled property is set to true.

9. SpringApplication.run(): Boots the Spring Boot application and returns the application context which is used to get the FeatureService bean if the condition is met.


Comments