|
| 1 | +--- |
| 2 | +title: Enable conditional features with a custom filter in a Spring Boot application |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn how to implement a custom feature filter to enable conditional feature flags for your Spring Boot application. |
| 5 | +ms.service: azure-app-configuration |
| 6 | +ms.devlang: java |
| 7 | +ms.custom: devx-track-java |
| 8 | +author: mrm9084 |
| 9 | +ms.author: mametcal |
| 10 | +ms.topic: how-to |
| 11 | +ms.date: 02/11/2026 |
| 12 | +--- |
| 13 | + |
| 14 | +# Enable conditional features with a custom filter in a Spring Boot application |
| 15 | + |
| 16 | +Feature flags can use feature filters to enable features conditionally. To learn more about feature filters, see [Enable conditional features with feature filters](./howto-feature-filters.md). |
| 17 | + |
| 18 | +The example used in this guide is based on the Spring Boot application introduced in the feature management [quickstart](./quickstart-feature-flag-spring-boot.md). Before proceeding further, complete the quickstart to create a Spring Boot application with a *Beta* feature flag. Once completed, you must [add a custom feature filter](./howto-feature-filters.md) to the *Beta* feature flag in your App Configuration store. |
| 19 | + |
| 20 | +In this article, you learn how to implement a custom feature filter and use the feature filter to enable features conditionally. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- Create a [Spring Boot app with a feature flag](./quickstart-feature-flag-spring-boot.md). |
| 25 | +- [Add a custom feature filter to the feature flag](./howto-feature-filters.md) |
| 26 | + |
| 27 | +## Implement a custom feature filter |
| 28 | + |
| 29 | +You've added a custom feature filter named **Random** with a **Percentage** parameter for your *Beta* feature flag in the prerequisites. Next, you implement the feature filter to enable the *Beta* feature flag based on the chance defined by the **Percentage** parameter. |
| 30 | + |
| 31 | +1. Add a `RandomFilter.java` file in the package directory of your application with the following code. |
| 32 | + |
| 33 | + ```java |
| 34 | + import java.util.Random; |
| 35 | + |
| 36 | + import com.azure.spring.cloud.feature.management.filters.FeatureFilter; |
| 37 | + import com.azure.spring.cloud.feature.management.models.FeatureFilterEvaluationContext; |
| 38 | + |
| 39 | + import org.springframework.stereotype.Component; |
| 40 | + |
| 41 | + @Component("Random") |
| 42 | + public class RandomFilter implements FeatureFilter { |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean evaluate(FeatureFilterEvaluationContext context) { |
| 46 | + Object value = context.getParameters().get("Percentage"); |
| 47 | + int percentage = value != null ? Integer.parseInt(value.toString()) : 0; |
| 48 | + int random = new Random().nextInt(100); |
| 49 | + return random < percentage; |
| 50 | + } |
| 51 | + } |
| 52 | + ``` |
| 53 | + |
| 54 | + You added a `RandomFilter` class that implements the `FeatureFilter` interface from the `spring-cloud-azure-feature-management` library. The `FeatureFilter` interface has a single method named `evaluate`, which is called whenever a feature flag is evaluated. In `evaluate`, a feature filter enables a feature flag by returning `true`. |
| 55 | + |
| 56 | + You decorated the class with `@Component("Random")` to register it as a Spring bean with the name **Random**, which matches the filter name you set in the *Beta* feature flag in Azure App Configuration. |
| 57 | + |
| 58 | +1. Open your main application class or controller and add code to access the *Beta* feature flag a few times: |
| 59 | + |
| 60 | + ```java |
| 61 | + import org.springframework.boot.CommandLineRunner; |
| 62 | + import org.springframework.boot.SpringApplication; |
| 63 | + import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 64 | + import org.springframework.context.annotation.Bean; |
| 65 | + |
| 66 | + import com.azure.spring.cloud.feature.management.FeatureManager; |
| 67 | + |
| 68 | + @SpringBootApplication |
| 69 | + public class DemoApplication { |
| 70 | + |
| 71 | + public static void main(String[] args) { |
| 72 | + SpringApplication.run(DemoApplication.class, args); |
| 73 | + } |
| 74 | + |
| 75 | + @Bean |
| 76 | + public CommandLineRunner runner(FeatureManager featureManager) { |
| 77 | + return args -> { |
| 78 | + for (int i = 0; i < 10; i++) { |
| 79 | + System.out.println("Beta is " + featureManager.isEnabled("Beta")); |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + } |
| 84 | + ``` |
| 85 | + |
| 86 | +## Feature filter in action |
| 87 | + |
| 88 | +When you run the application, the configuration provider loads the *Beta* feature flag from Azure App Configuration. The result of the `isEnabled("Beta")` method is printed to the console. Since the `RandomFilter` is used by the *Beta* feature flag and is configured to 50 percent, the result is `True` 50 percent of the time and `False` the other 50 percent of the time. |
| 89 | + |
| 90 | +Running the application shows that the *Beta* feature flag is sometimes enabled and sometimes not. |
| 91 | + |
| 92 | +```bash |
| 93 | +Beta is true |
| 94 | +Beta is false |
| 95 | +Beta is true |
| 96 | +Beta is true |
| 97 | +Beta is true |
| 98 | +Beta is false |
| 99 | +Beta is false |
| 100 | +Beta is false |
| 101 | +Beta is true |
| 102 | +Beta is true |
| 103 | +``` |
| 104 | + |
| 105 | +## Next steps |
| 106 | + |
| 107 | +To learn more about the built-in feature filters, continue to the following documents. |
| 108 | + |
| 109 | +> [!div class="nextstepaction"] |
| 110 | +> [Enable features on a schedule](./howto-timewindow-filter.md) |
| 111 | +
|
| 112 | +> [!div class="nextstepaction"] |
| 113 | +> [Roll out features to targeted audience](./howto-targetingfilter.md) |
0 commit comments