Skip to content

Instantly share code, notes, and snippets.

@Kurukshetran
Forked from milanboers/MyComponent.java
Created March 7, 2019 13:34
Show Gist options
  • Save Kurukshetran/3ad40547daa48d90d455eafaf7310bb6 to your computer and use it in GitHub Desktop.
Save Kurukshetran/3ad40547daa48d90d455eafaf7310bb6 to your computer and use it in GitHub Desktop.
Enable/disable Spring component by property
@Component
@Conditional(MyComponent.EnabledCondition.class)
public class MyComponent {
protected static class EnabledCondition implements Condition {
private static final String ENABLED_PROPERTY = "mycomponent.enabled";
@Override
public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
final Environment environment = context.getEnvironment();
return environment != null && Boolean.valueOf(environment.getProperty(ENABLED_PROPERTY));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment