Skip to content

Commit 854b821

Browse files
authored
Merge pull request #311694 from mrm9084/SpringConditionalFeatureFilters
App Config Spring Custom Feature Filters
2 parents bb61231 + 0d091d4 commit 854b821

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

articles/azure-app-configuration/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@
212212
href: howto-feature-filters.md
213213
- name: ASP.NET Core
214214
href: howto-feature-filters-aspnet-core.md
215+
- name: Spring Boot
216+
href: how-to-feature-filters-spring-boot.md
215217
- name: JavaScript
216218
href: howto-feature-filters-javascript.md
217219
- name: Python
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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)

articles/azure-app-configuration/howto-feature-filters.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ You can create custom feature filters that turn on features based on specific cr
7070
1. To implement the feature filter in your application, see the instructions that are appropriate for your language or platform:
7171

7272
- [ASP.NET Core](./howto-feature-filters-aspnet-core.md)
73+
- [Spring Boot](./how-to-feature-filters-spring-boot.md)
7374
- [Node.js](./howto-feature-filters-javascript.md)
7475
- [Python](./howto-feature-filters-python.md)
7576
- [Go Gin](./howto-feature-filters-go.md)

0 commit comments

Comments
 (0)