user3139545 :
I would like to do something as the following to conditional load a bean:
@Conditional(OnAuthoritieCondition.class, "SOME_INPUT_STRING")
However when I try to implement this I get the following:
public class OnAuthoritieCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return false;
}
}
Now my question is how can I implement a custom condition that takes parameters or is there a better way to do what I want?
Dirk Deyne :
create your custom annotation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Conditional(value = CustomCondition.class)
public @interface ConditionalController {
String condition();
}
your custom condition
public class CustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalController.class.getName());
String condition = (String) attributes.get("condition");
return "GO".equalsIgnoreCase(condition);
}
}
use it like
@ConditionalController(condition = "GO")