@Conditional系列注解例子
1. @Conditional
说明:指定的Condition实现类,matches方法返回true则注入bean,false则不注入
@Configuration
public class BeanConfig { //只有一个类时,大括号可以省略
//如果WindowsCondition的实现方法返回true,则注入这个bean
@Conditional({WindowsCondition.class})
@Bean(name = "bill")
public Person person1(){
return new Person("Bill Gates",62);
} //如果LinuxCondition的实现方法返回true,则注入这个bean
@Conditional({LinuxCondition.class})
@Bean("linus")
public Person person2(){
return new Person("Linus",48);
}
}
创建 LinuxCondition和 WindowsCondition类,并实现Condition接口
public class LinuxCondition implements Condition { @Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { Environment environment = conditionContext.getEnvironment(); String property = environment.getProperty("os.name");
if (property.contains("Linux")){
return true;
}
return false;
}
}
2. @ConditionalOnBean
说明:仅仅在当前上下文中存在某个对象时,才会实例化一个Bean
//RedisOperBean依赖redisTemplate
@Component
@ConditionalOnBean(name="redisTemplate")
public class RedisOperBean {
private final RedisTemplate redisTemplate;
public RedisOperBean(RedisTemplate redisTemplate) {
// ...
}
}
3. @ConditionalOnClass
说明:某个class位于类路径上,才会实例化一个Bean,要求指定的class必须存在
4. @ConditionalOnProperty
说明:
(1)matchIfMissing:从application.properties中读取某个属性值,如果该值为空,默认值为true
(2)havingValue:通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值,如果该值为空,则返回false;
如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。
@Configuration
@ConditionalOnClass({ Feign.class })
@ConditionalOnProperty(value = "feign.oauth2.enabled", havingValue = "true", matchIfMissing = true)
public class OAuth2FeignAutoConfiguration { @Bean
@ConditionalOnBean(OAuth2ClientContext.class)
public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext) {
return new OAuth2FeignRequestInterceptor(oauth2ClientContext);
}
}
5.@ConditionalOnMissingBean
说明:仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean
@Bean
@ConditionalOnMissingBean(name = "imageValidateCodeGenerator")
public ValidateCodeGenerator imageValidateCodeGenerator() {
ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
codeGenerator.setSecurityProperty(securityProperties);
return codeGenerator;
}
6.@ConditionalOnMissingClass
说明:某个class类路径上不存在的时候,才会实例化一个Bean
7.@ConditionalOnExpression
说明:当表达式为true的时候,才会实例化一个Bean
@Configuration
@ConditionalOnExpression("${enabled:false}")
public class BigpipeConfiguration {
@Bean
public OrderMessageMonitor orderMessageMonitor(ConfigContext configContext) {
return new OrderMessageMonitor(configContext);
}
}
其他样式:
@ConditionalOnExpression("${mq.cumsumer.enabled}==1&&${rabbitmq.comsumer.enabled:true}") @ConditionalOnExpression("'${mq.comsumer}'.equals('rabbitmq')")
8.@ConditionalOnNotWebApplication
说明:当前不是web应用
9.@ConditionalOnWebApplication
说明:当前是web应用
10. @ConditionalOnResource
说明:类路径下是否存在指定的资源文件
@Bean
@ConditionalOnResource(resources="classpath:shiro.ini")
protected Realm iniClasspathRealm(){ }
最新文章
- [LeetCode] Merge Intervals 合并区间
- gradle
- 转换成的jar文件接收后台的信息乱码cmd解决办法
- 解决 Django 后台上传图片前端无法展示
- 手机注册获取验证码的PHP代码
- [转]http://makefiletutorial.com/
- IOS第五天(2:用户登录,回车的监听(代理模式UITextFieldDelegate)) 和关闭键盘
- 老师你好。使用cordova生成的hellowold 的安卓5.0版本太高。怎么才可以生成4.4的呢?
- http://www.cnblogs.com/leiOOlei/p/5075402.html
- this指针指向的彻底理解
- Linux kill -9 和 kill -15 的区别
- linux的学习系列 7---管道和过滤器
- ReactNative实现图集功能
- Http请求小结
- 17 python 初学(迭代器)
- ES6的Promise浅析
- [dev] 啥是Virtual Private Network
- python的条件语句
- 如何实现session跨服务器共享
- HTML5关于上传API的一些使用(上)
热门文章
- windows xp .net framework 4.0 HttpWebRequest 报The underlying connection was closed,基础连接已关闭
- Java中date和calendar的用法
- Shell内置命令expr
- 基于Libpcap实现一个网络数据包嗅探器
- glog 与 zlog
- 四大开源协议比较:BSD、Apache、GPL、LGPL(转)
- python爬虫学习(3):使用User-Agent和代理ip
- nginx获取头部信息带下划线,获取不到解决方案
- clickhouse核心引擎MergeTree子引擎
- 对称性——cf405d