0 day streak
java / beginner
Snippet

Toggle Features with @ConditionalOnProperty

Use @ConditionalOnProperty to load a Bean only if a specific configuration property exists and matches a certain value.

snippet.java
java
1
2
3
4
5
@Component
@ConditionalOnProperty(name = "app.feature.sms", havingValue = "true")
public class SmsService {
// Implementation logic here
}
spring
Breakdown
1
@ConditionalOnProperty(name = "app.feature.sms", ...)
Checks for the property 'app.feature.sms' in application.properties.
2
havingValue = "true"
The component is only registered if the property value is exactly 'true'.