1、 @Conditional 注解

基于条件的注解,它的作用是根据是否满足某个特定条件来决定是否创建某个特定的Bean,他是Spring Boot 实现自动配置的基础能力

Indicates that a component is only eligible for registration when all specified conditions match.
指示组件只有在所有指定条件匹配时才有资格注册。
A condition is any state that can be determined programmatically before the bean definition is due to be registered
(see Condition for details).
条件是在注册bean定义之前可以通过编程方式确定的任何状态(详细信息请参阅条件)。
The @Conditional annotation may be used in any of the following ways:
@Conditional注释可以用以下几种方式使用:
as a type-level annotation on any class directly or indirectly annotated with @Component, including @Configuration
classes as a meta-annotation, for the purpose of composing custom stereotype annotations as a method-level
annotation on  any @Bean method If a @Configuration class is marked with @Conditional, all of the @Bean methods,
@Import annotations,  and @ComponentScan annotations associated with that class will be subject to the conditions.
类型层次注释在任何类直接或间接与@component注释,包括元注释@configuration类编写自定义原型为目的的任何@bean注释的方法级注释方法
如果标有@configuration类@Conditional, @bean方法,所有的注释 @import, @ComponentScan注释相关的类将受到条件。
NOTE: Inheritance of @Conditional annotations is not supported;
注意:不支持继承@Conditional注释;
any conditions from superclasses or from overridden methods will not be considered.
来自超类或覆盖方法的任何条件都将不被考虑。
In order to enforce these semantics, @Conditional itself is not declared as @Inherited;
为了加强这些语义,@Conditional本身没有被声明为@Inherited;
furthermore, any custom composed annotation that is meta-annotated with @Conditional must not be declared
as @Inherited.
此外,任何用@Conditional元注释的自定义组合注释都不能声明为@Inherited。@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {/*** All {@link Condition}s that must {@linkplain Condition#matches match}* in order for the component to be registered.*/Class<? extends Condition>[] value();
}

2、常见Conditional注解

2.1、ConditionalOnBean

当容器中有指定的 Bean 才开启配置,如何实现的呢?内部原理是什么?


@Conditional(OnBeanCondition.class)
public @interface ConditionalOnBean {...
}

2.2、ConditionalOnClass

@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {...
}

2.3、ConditionalOnMissingBean

@Conditional(OnBeanCondition.class)
public @interface ConditionalOnMissingBean {...
}

2.4、ConditionalOnNotWebApplication

@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnNotWebApplication {...
}

2.5、ConditionalOnProperty

@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {...
}

2.6、ConditionalOnJava

public @interface ConditionalOnJava {...
}

2.7、ConditionalOnWebApplication

public @interface ConditionalOnWebApplication {...
}

2.8、ConditionalOnNotWebApplication

@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnNotWebApplication {...
}

3、自定义@Conditional注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(FirstCondition.class)
public @interface FirstConditionAnnotation {String[] values() default{};
}public class FirstCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Map<String, Object> valueMap = metadata.getAnnotationAttributes(FirstConditionAnnotation.class.getName());String[] propertykeys = (String[])valueMap.get("values");for (String key : propertykeys) {String value = context.getEnvironment().getProperty(key);if (StringUtils.isEmpty(value)) {return false;}}return true;}
}
@Component
@FirstConditionAnnotation(values={"firstCondition1","firstCondition2"})
public class FirstConditionBean {}

12.1、自定义Condition相关推荐

  1. java 多线程缓存_[Java教程]【JAVA并发编程实战】12、使用condition实现多线程下的有界缓存先进先出队列...

    [Java教程][JAVA并发编程实战]12.使用condition实现多线程下的有界缓存先进先出队列 0 2016-11-29 17:00:10 package cn.study.concurren ...

  2. windows phone (12) 小试自定义样式

    windows phone (12) 小试自定义样式 原文:windows phone (12) 小试自定义样式 样式在BS开发中经常用到,在wp中系统也提供了解决办法,就是对设置的样式的一种资源共享 ...

  3. 12.zabbix 自定义模板

    文章目录 12. zabbix 自定义模板 12.1 创建应用集 12.2 创建监控项 12.3 创建模板 12.4 引用模板 12.5 导出模板 12. zabbix 自定义模板 12.1 创建应用 ...

  4. android 11.0 12.0自定义开机向导app

    1.概述 在11.0 12.0的系统产品开发中,需要自定义开机向导app页面,而系统源码中只提供了Provision作为开机向导app 有些平台没有把它编译到源码中 作为开机向导,所以自定义开机向导, ...

  5. pyqt5讲解12:自定义参数 (给信号传入参数)

    在pyqt编程过程中,经常会遇到给槽函数传递自定义参数的情况,比如有一个信号与槽函数的连接是 button1.clicked.connect(shou_page) 对于clicked 信号来说,是没有 ...

  6. (12)自定义数据流(实战Docker事件推送的REST API)——响应式Spring的道法术器...

    本系列文章索引<响应式Spring的道法术器> 前情提要 Reactor 3快速上手 | Spring WebFlux快速上手 | 响应式流规范 本文 测试源码 | 实战源码 2.2 自定 ...

  7. 12.0 自定义SystemUI下拉状态栏和通知栏视图(二十)之锁屏通知布局

    1.前言 在进行12.0的系统rom产品定制化开发中,在12.0中针对systemui下拉状态栏和通知栏的定制UI的工作开发中,原生系统的下拉状态栏和通知栏的视图UI在产品开发中会不太满足功能, 所以 ...

  8. struts2教程:12、自定义类型转换器

    自定义类型转换器 有两种:1.局部.2.全局 java.util.Date类型的属性可以接收格式为2009-07-20的请求参数值.但如果我们需要接收格式为20091221的请求参数,我们必须定义类型 ...

  9. Android 12.0 自定义仿小米全面屏手势导航左右手势滑动返回UI效果

    目录 1.概述 2.自定义仿小米全面屏手势导航左右手势滑动返回UI效果的核心类

最新文章

  1. Linux SSH无交互式访问配置
  2. 数据库的定义、模型 和 模式
  3. ubuntu15.04配置php,Linux_Ubuntu 15.04上安装Justniffer的详细教程,Justniffer 是一个可用于替代 Snor - phpStudy...
  4. Node.js抓取网页信息并展示(cheerio网络爬虫)
  5. Pytest之重运行及生成测试报告
  6. 极速理解设计模式系列:10.抽象工厂模式(Abstract Factory Pattern)
  7. C#中英文逗号之间的相互转化
  8. 大华股份携手阿里云计算 涉足智能家居
  9. 递归算法—输入字母逆序输出汉诺塔递归算法
  10. [2018.10.31 T3] 玩
  11. Could not find com.android.tools.build:gradle:2.2.3
  12. 01-移植AZPR SoC到Xilinx Aritix7 FPGA
  13. Android开发入门案例
  14. 开发时解决数据回显的小方法
  15. Windows Server 2008 R2 官方原版下载大全
  16. 林语堂的《武则天传》读后感
  17. java cnzz_cnzz 模拟请求登录(传入url get data ) demo
  18. 通过server酱来发送爬虫爬到的数据
  19. Java 编程问题:二、对象、不变性和`switch`表达式
  20. ES6中setTimeout函数的this

热门文章

  1. Visual Studio 编写纯C语言程序
  2. python 旋转矩阵_【每日算法Day 93】不用额外空间,你会旋转一个矩阵吗?
  3. 介绍计算机的英文文章,计算机方面的英语资料,介绍一些计算机的英语短文,有兴趣的可以看...
  4. Spring Boot接口返回的字段名和实体类中定义的字段名不一致
  5. CentOS7中解决root用户Operation not permitted
  6. acill排序 java_字符串ASCII码排序
  7. Android系统运行库分为,Android系统架构
  8. java 下载管理_Java多线程文件下载管理器详解
  9. 浪潮通信收入_浪潮获2019大数据“星河(Galaxy)”奖
  10. jpa 托管_JPA EntityManager详解(一)