Skip to content

Instantly share code, notes, and snippets.

@milanboers
Created July 31, 2017 13:18
Show Gist options
  • Save milanboers/f95240122a756ea9b23f88d136671ee3 to your computer and use it in GitHub Desktop.
Save milanboers/f95240122a756ea9b23f88d136671ee3 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