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系列注解例子的更多相关文章

  1. [转帖]Java高级系列——注解(Annotations)

    Java高级系列——注解(Annotations) 2018年01月13日 :: RonTech 阅读数 3405更多 所属专栏: Java高级系列文章 版权声明:转载请注明出处,谢谢配合. http ...

  2. Spring源码系列 — 注解原理

    前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...

  3. Spring5源码深度分析(二)之理解@Conditional,@Import注解

    代码地址: 1.源码分析二主要分析的内容 1.使用@Condition多条件注册bean对象2.@Import注解快速注入第三方bean对象3.@EnableXXXX 开启原理4.基于ImportBe ...

  4. 简单的JPA注解例子

    package ssh.entity; import java.math.BigDecimal; import java.util.Date; import javax.persistence.*; ...

  5. Android Annotations 注解例子

    1.AndroidAnnotations官网: http://androidannotations.org/ (也许你需要FQ) 2.eclipse中使用androidannotations的配置方法 ...

  6. 注解Annotation实现原理与自定义注解例子

    什么是注解? 对于很多初次接触的开发者来说应该都有这个疑问?Annontation是Java5开始引入的新特征,中文名称叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metada ...

  7. 【转】java编程思想第20章的注解例子用到的com.sun.mirror的jar包

    Java編程思想>中的注解代码中引入过这么一个包(com.sun.mirror),书上说的是在Jdk中有个tools.jar中,引入这个包就每这个问题了,但是笔者用的是JDK 1.8,把这个包i ...

  8. Java开发系列-注解

    概述 在JDK5之后提供了一个新特性,和类.接口同级.定义时使用的关键字是@interface.注解主要有三个作用,分别是编译检查.替代配置文件.定义注解(元注解).分析代码(用到反射).注解的本质就 ...

  9. Hadoop概念学习系列之例子形象再谈Client、NameNode、元数据(三十一)

    Client相当于是送货人或提货人. NameNode相当于是仓库管理员. 元数据相当于是账本清单.

随机推荐

  1. python学习之路,2018.8.9

    python学习之路,2018.8.9, 学习是一个长期坚持的过程,加油吧,少年!

  2. [轉]C/C++中的volatile使用時機?

    不知各位對volatile(揮發性的)這個字陌不陌生? 我相信大家在一些程式或多或少都看 過這個字眼, 但是究竟要在何種場合用它呢?.當然一定是有需要, C/C++才會有這個保留字, 否則只是增加pr ...

  3. 与JS报错的那段时光

    1.Uncaught SyntaxError: Unexpected end of input js报错: 翻译:语法错误:输入意外终止 原因:页面代码写的不规范  ╮(╯▽╰)╭ 其中的某条语句,没 ...

  4. CentOs如何找回root密码

    当CentOs的root密码忘了的时候,可以进入单用户模式,更改一下root密码就可以了. 具体操作如下 1.重启Linux后,在此界面,3秒内按下回车键 2.出现此界面 3.按e键,进入下图,然后把 ...

  5. dubbo-源码阅读之服务订阅

    配置例子 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  6. rest framework之限流组件

    一.自定义限流 限流组件又叫做频率组件,用于控制客户端可以对API进行的请求频率,比如说1分钟访问3次,如果在1分钟内超过3次就对客户端进行限制. 1.自定义限流 假设现在对一个API访问,在30s内 ...

  7. redis集群添加新节点

    一.创建节点(接上文) 1.在H1服务器/root/soft目录下创建7002目录 2.将7001目录的配置文件redis.conf拷贝到7002,并修改配置文件的端口 3.进入 redis-5.0. ...

  8. 【JavaWeb项目】一个众筹网站的开发(八)后台页面详细设置

    一.user.jsp改造 删除引入菜单 抽取导航栏 nav-bar.jsp,删除引入导航栏 删除引入main.jsp的到好烂 数据库里添加url 报错,url不对 没有/ url正确 action=& ...

  9. 使用pangolin库画出轨迹

    https://github.com/stevenlovegrove/Pangolin cmake_minimum_required(VERSION 2.8) project(chapter3) ) ...

  10. console.log(([])?true:false); console.log(([]==false?true:false)); console.log(({}==false)?true:false)

    下面是题目的类型转换结果: Boolean([]); //true Number([]); //0 Number({}); // NaN Number(false); //0 因此: console. ...