Spring-boot中Conditional介绍
来源:互联网 发布:百度网盘会员 淘宝 编辑:程序博客网 时间:2024/06/09 18:28
原文地址:http://www.jianshu.com/p/0740c07f6c1d
@Conditional
官方文档定义:“Indicates that a component is only eligible for registration when all specified conditions match”,意思是只有满足一些列条件之后创建一个bean。@Conditional定义
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE, ElementType.METHOD})public @interface Conditional { Class<? extends Condition>[] value();}@Conditional注解主要用在以下位置:
- 类级别可以放在注标识有@Component(包含@Configuration)的类上
- 作为一个meta-annotation,组成自定义注解
- 方法级别可以放在标识由@Bean的方法上
spring-boot的@Conditional系列
spring-boot利用Conditional来确定是不是要创建Bean实例,如下是官方的说明,大概是说boot是启用@Conditional注解来确定是否要加载该实例
No. Boot is enabled in Spring Framework 4.0 by the @Conditional annotation infrastructure. Your perception of loaded is accurate otherwise - JRebel is much more comprehensive, works with multiple IDEs, etc.
- ConditionalOnBean: 当且仅当指定的bean classes and/or bean names在当前容器中,才创建标记上该注解的类的实例
- ConditionalOnMissingBean: 当且仅当指定的bean classes and/or bean names不存在当前容器中,才创建标记上该注解的类的实例,有指定忽略ignored的参数存在,可以忽略Class、Type等
- ConditionalOnClass:当且仅当ClassPath存在指定的Class时,才创建标记上该注解的类的实例
- ConditionalOnMissingClass:当且仅当ClassPath不存在指定的Class时,创建标记上该注解的类的实例
- ConditionalOnProperty:当且仅当Application.properties存在指定的配置项时,创建标记上了该注解的类的实例
- ConditionalOnJava:指定JDK的版本
- ConditionalOnExpression:表达式用${..}=false等来表示
- ConditionalOnJndi:JNDI存在该项时创建
- ConditionalOnResource:在classpath下存在指定的resource时创建
- ConditionalOnSingleCandidate:Conditional that only matches when the specified bean class is already contained in the BeanFactory and a single candidate can be determined.The condition will also match if multiple matching bean instances are already contained in the BeanFactory but a primary candidate has been defined; essentially, the condition match if auto-wiring a bean with the defined type will succeed.
- ConditionalOnWebApplication:在web环境下创建
阅读全文
0 0
- Spring-boot中Conditional介绍
- Spring boot源码分析-Conditional(12)
- spring boot 条件注解的使用@Conditional;@Conditional 可以根据条件,创建不同的bean,
- spring @conditional
- 浅谈spring中@Conditional(条件注解)
- Spring Boot简单介绍
- II. Spring Boot介绍
- Spring Boot 配置文件介绍
- Spring-boot介绍
- Spring boot 框架介绍
- Spring boot 入门介绍
- Spring Boot介绍
- Spring Boot 的介绍
- Spring boot 介绍说明
- Spring Boot 配置文件介绍
- Spring Boot Starter 介绍
- Spring boot admin介绍
- Spring Boot框架介绍
- SylixOS调试与性能分析技术--内存泄漏检测
- jsp中Double类数字太大时页面正常显示而不要用科学计数法
- 25岁Java工程师如何转型学习人工智能?
- 全面解析JavaScript Module模式
- Spring 数据库数据源DBCP配置说明
- Spring-boot中Conditional介绍
- 几何题
- mysql插入数据抛出DuplicateKeyException导致无法插入
- 1.jdk8新特性概述
- Maven
- iOS:CAAnimation动画集合:移动/旋转/缩放/弹簧/翻页效果/落叶动画
- Android图片点击放大等
- IDEA创建基于springboot的多个module聚合项目,并生成war包
- 推荐一本好书:C++反汇编与逆向分析技术揭秘