spring_boot_cloud(14)Condition条件注解
Condition
A single condition that must be matched in order for a component to be registered.
用来标示一个组件是否可以被注册。
Conditional
Condition和Conditional成对出现
1 | @Target({ElementType.TYPE, ElementType.METHOD}) |
实例
我们在application.yml里边添加一个配置:
1 | person: |
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;
}
}
1 | 如果城市是天津就返回true,表示加载。 |
@Configuration
public class TestConditionConfig {
//Conditional的参数TestCondition规定了是否可以加载这个bean,中间会调TestCondition的matches方法
@Bean
@Conditional(TestCondition.class)
public String getConfig(){
return "hello";
}
//没有配置Conditional条件加载
@Bean
public String getConfig2(){
return "world";
}
@Bean
@Conditional(TestCondition.class)
public String getConfig3(){
return "welcome";
}
}
1 |
|
@SpringBootApplication
public class MyApplication {
private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(MyApplication.class,args);
String [] names = configurableApplicationContext.getBeanNamesForType(String.class);
System.out.println(names);
}
}
1 | 输出: |
person:
address:
city: dalian
name:
firstNamme: zhangsan
1 | 创建新的Condition: |
public class TestCondition2 implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String city = context.getEnvironment().getProperty(“person.name.firstNamme”);
if (“lisi”.equals(city)) {
return true;
}
return false;
}
}
1 | 修改TestConditionConfig的getConfig3 |
@Bean
@Conditional({TestCondition.class, TestCondition2.class})
public String getConfig3(){
return “welcome”;
}
运行MyApplication输出:
[getConfig, getConfig2]
#### 小结
Conditional要求Conditional.value()返回的值都返回true,才会加载。
#### Conditional在spring框架内部的使用
ConditionalOnBean Bean存在的条件加载
ConditionalOnClass Class存在的条件加载
ConditionalOnCloudPlatform
ConditionalOnExpression
ConditionalOnJava
ConditionalOnJndi
ConditionalOnMissingBean 当Bean不存在的条件加载
ConditionalOnMissingClass 当Class不存在的条件加载
ConditionalOnNotWebApplication
ConditionalOnProperty
ConditionalOnResource
ConditionalOnSingleCandidate
ConditionalOnWebApplication
ConditionEvaluationReport
这些注解实现了springboot的按需加载。