A single condition that must be matched in order for a component to be registered. 用来标示一个组件是否可以被注册。
Conditional
Condition和Conditional成对出现
1 2 3 4 5 6 7 8 9 10 11 12 13
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional {
/** * All {@link Condition Conditions} that must {@linkplain Condition#matches match} * in order for the component to be registered. */ //所有Condition都匹配才可以被注册进来 Class<? extends Condition>[] value();
}
实例
我们在application.yml里边添加一个配置:
1 2 3 4
person: address: city: dalian `
创建一个实现了Condition的类:
1 2 3 4 5 6 7 8 9 10
public class TestCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { String city = context.getEnvironment().getProperty("person.address.city"); if("tianjin".equals(city)){ return true; } return false; } }