java
45 lines · 7 steps
A custom @Conditional feature flag in Spring
A Spring Condition that toggles bean registration based on a feature flag property and active profiles.
Explained by
highlit
1package com.example.config.condition;
2
3import org.springframework.context.annotation.Condition;
4import org.springframework.context.annotation.ConditionContext;
5import org.springframework.core.env.Environment;
6import org.springframework.core.type.AnnotatedTypeMetadata;
7
8import java.util.Map;
9
10public class OnFeatureFlagCondition implements Condition {
11
12 @Override
13 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
14 Map<String, Object> attributes =
15 metadata.getAnnotationAttributes(ConditionalOnFeature.class.getName());
16 if (attributes == null) {
17 return false;
18 }
19
20 String feature = (String) attributes.get("value");
21 boolean expected = (boolean) attributes.get("enabledByDefault");
22
23 Environment env = context.getEnvironment();
24 String property = "features." + feature + ".enabled";
25 boolean enabled = env.getProperty(property, Boolean.class, expected);
26
27 String[] requiredProfiles = (String[]) attributes.get("activeProfiles");
28 if (requiredProfiles.length > 0 && !anyProfileActive(env, requiredProfiles)) {
29 return false;
30 }
31
32 return enabled;
33 }
34
35 private boolean anyProfileActive(Environment env, String[] profiles) {
36 for (String profile : profiles) {
37 for (String active : env.getActiveProfiles()) {
38 if (active.equalsIgnoreCase(profile)) {
39 return true;
40 }
41 }
42 }
43 return false;
44 }
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing Condition lets you gate bean registration on arbitrary runtime logic beyond Spring's built-in conditions.
- 2Reading annotation attributes via AnnotatedTypeMetadata connects a custom annotation to its evaluation logic.
- 3Combining a property lookup with a profile check produces flexible, layered activation rules.
Related explainers
java
public List<OrderSummary> streamRecentOrders(LocalDateTime since, Consumer<OrderSummary> handler) { String sql = """ SELECT id, customer_id, total_cents, status, created_at FROM orders
Streaming large JDBC result sets safely
jdbc
streaming
resource-management
Intermediate
7 steps
java
public class DuplicateFinder { public Map<String, List<Path>> findDuplicates(Path root) throws IOException { Map<String, List<Path>> byHash = new HashMap<>();
Finding duplicate files by content hash
hashing
file-io
streams
Intermediate
8 steps
java
@Component public class OrderRecoveryHandler { private final RabbitTemplate rabbitTemplate;
Dead-letter recovery with Spring AMQP
dead-letter-queue
retry
exponential-backoff
Advanced
9 steps
java
@Component public class CorrelationIdWebFilter implements WebFilter, Ordered { public static final String CORRELATION_ID_HEADER = "X-Correlation-Id";
Correlation IDs in a Spring WebFlux filter
reactive
web filter
correlation id
Advanced
7 steps
java
public final class DeepCopier { private static final ObjectMapper MAPPER = new ObjectMapper() .registerModule(new JavaTimeModule())
Deep-copying objects via Jackson serialization
serialization
deep-copy
generics
Intermediate
7 steps
java
package com.example.time; import java.time.Duration; import java.time.Instant;
Adding ISO-8601 durations across time zones in Java
date-time
iso-8601
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-custom-conditional-feature-flag-in-spring-explained-java-9c5a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.