@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(){ }
@Conditional系列注解例子的更多相关文章
- [转帖]Java高级系列——注解(Annotations)
Java高级系列——注解(Annotations) 2018年01月13日 :: RonTech 阅读数 3405更多 所属专栏: Java高级系列文章 版权声明:转载请注明出处,谢谢配合. http ...
- Spring源码系列 — 注解原理
前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...
- Spring5源码深度分析(二)之理解@Conditional,@Import注解
代码地址: 1.源码分析二主要分析的内容 1.使用@Condition多条件注册bean对象2.@Import注解快速注入第三方bean对象3.@EnableXXXX 开启原理4.基于ImportBe ...
- 简单的JPA注解例子
package ssh.entity; import java.math.BigDecimal; import java.util.Date; import javax.persistence.*; ...
- Android Annotations 注解例子
1.AndroidAnnotations官网: http://androidannotations.org/ (也许你需要FQ) 2.eclipse中使用androidannotations的配置方法 ...
- 注解Annotation实现原理与自定义注解例子
什么是注解? 对于很多初次接触的开发者来说应该都有这个疑问?Annontation是Java5开始引入的新特征,中文名称叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metada ...
- 【转】java编程思想第20章的注解例子用到的com.sun.mirror的jar包
Java編程思想>中的注解代码中引入过这么一个包(com.sun.mirror),书上说的是在Jdk中有个tools.jar中,引入这个包就每这个问题了,但是笔者用的是JDK 1.8,把这个包i ...
- Java开发系列-注解
概述 在JDK5之后提供了一个新特性,和类.接口同级.定义时使用的关键字是@interface.注解主要有三个作用,分别是编译检查.替代配置文件.定义注解(元注解).分析代码(用到反射).注解的本质就 ...
- Hadoop概念学习系列之例子形象再谈Client、NameNode、元数据(三十一)
Client相当于是送货人或提货人. NameNode相当于是仓库管理员. 元数据相当于是账本清单.
随机推荐
- TreeMap源码解析笔记
常见的数据结构有数组.链表,还有一种结构也很常见,那就是树.前面介绍的集合类有基于数组的ArrayList,有基于链表的LinkedList,还有链表和数组结合的HashMap,今天介绍基于树的Tre ...
- Codeforces 490D Chocolate
D. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- CTU OPEN 2017 Go Northwest! /// 简单公式
题目大意: 给定n个点 求任选一对点连成的直线斜率为1或-1的概率 对于点(x1,y1) 和 点(x2,y2) 斜率 k=(y1-y2)/(x1-x2) 当k=1 则 (y1-y2)/(x1-x2)= ...
- SQL查询条件生成小工具
最近运维数据,经常遇到需要在sql条件中个In('',''....)个字符串的情况,于是在网上找了个小工具改造一下,先用着: 效果如图: using System; using System.Coll ...
- springboot整合RocketMq(非事务)
1.配置文件 1.yml配置文件 rocketmq: #mq配置 producer: iseffect: true type: default # (transaction,default) tran ...
- Android 导致OOM的常见原因
OOM主要有两种原因导致: 1. 加载大图片: 2. 内存泄漏: 一.加载大图片 在Android应用中加载Bitmap的操作是需要特别小心处理的,因为Bitmap会消耗很多内存.比如,Galaxy ...
- windows系统exe文件图标变成了白色无图标
转载:https://blog.csdn.net/whatday/article/details/52658412 在命令提示符下输入下列命令即可恢复. 按键 “WIN+R” 输入即可cmd ...
- JAXB读写xml
一.注解 在Java模型中的创建与 xml 文件对应的节点和属性需要用注解来表示 @XmlRootElement 作用:将一个Java类映射为一段XML的根节点参数: name 定义这个根节点 ...
- hdu 5791 思维dp
题目描述: 求序列A,B的公共子序列个数: 基本思路: 想到了dp,选的状态也对,但是就是就是写不出状态转移方程,然后他们都出了,到最后我还是没出,很难受,然后主要是没有仔细考虑dp[i][j],dp ...
- Magento多语言注意事项
Magento一般使用CSV文件方式翻译国际化词条 一般用法是代码中: <?php echo Mage::helper("module_name")->__('Item ...