@data注解不生效_面试官:你经常在SpringBoot中使用的条件注解底层是如何实现的?你了解过吗?...
SpringBoot内部提供了特有的注解:条件注解(Conditional Annotation)。比如@ConditionalOnBean、@ConditionalOnClass、@ConditionalOnExpression、@ConditionalOnMissingBean等。
条件注解存在的意义在于动态识别(也可以说是代码自动化执行)。比如@ConditionalOnClass会检查类加载器中是否存在对应的类,如果有的话被注解修饰的类就有资格被Spring容器所注册,否则会被skip。
比如FreemarkerAutoConfiguration这个自动化配置类的定义如下:
这个自动化配置类被@ConditionalOnClass条件注解修饰,这个条件注解存在的意义在于判断类加载器中是否存在freemarker.template.Configuration和FreeMarkerConfigurationFactory这两个类,如果都存在的话会在Spring容器中加载这个FreeMarkerAutoConfiguration配置类;否则不会加载。
# 条件注解内部的一些基础
在分析条件注解的底层实现之前,我们先来看一下这些条件注解的定义。以@ConditionalOnClass注解为例,它的定义如下:
它有2个属性,分别是类数组和字符串数组(作用一样,类型不一样),而且被@Conditional注解所修饰,这个@Conditional注解有个名为values的Class extends Condition>[]类型的属性。 这个Condition是个接口,用于匹配组件是否有资格被容器注册,定义如下:
也就是说@Conditional注解属性中可以持有多个Condition接口的实现类,所有的Condition接口需要全部匹配成功后这个@Conditional修饰的组件才有资格被注册。
Condition接口有个子接口ConfigurationCondition:
这个子接口是一种特殊的条件接口,多了一个getConfigurationPhase方法,也就是条件注解的生效阶段。只有在ConfigurationPhase中定义的两种阶段下才会生效。
Condition接口有个实现抽象类SpringBootCondition,SpringBoot中所有条件注解对应的条件类都继承这个抽象类。它实现了matches方法:
# 基于Class的条件注解
SpringBoot提供了两个基于Class的条件注解:@ConditionalOnClass(类加载器中存在指明的类)或者@ConditionalOnMissingClass(类加载器中不存在指明的类)。
@ConditionalOnClass或者@ConditionalOnMissingClass注解对应的条件类是OnClassCondition,定义如下:
比如FreemarkerAutoConfiguration中的@ConditionalOnClass注解中有value属性是freemarker.template.Configuration.class和FreeMarkerConfigurationFactory.class。在OnClassCondition执行过程中得到的最终ConditionalOutcome中的log message如下:
1 @ConditionalOnClass classes found: freemarker.template.Configuration,org.springframework.ui.freemarker.FreeMarkerConfigurationFactory
@ConditionalOnBean(Spring容器中存在指明的bean)、@ConditionalOnMissingBean(Spring容器中不存在指明的bean)以及ConditionalOnSingleCandidate(Spring容器中存在且只存在一个指明的bean)都是基于Bean的条件注解,它们对应的条件类是ConditionOnBean。
@ConditionOnBean注解定义如下:
OnBeanCondition条件类的匹配代码如下:
SpringBoot还提供了其他比如ConditionalOnJava、ConditionalOnNotWebApplication、ConditionalOnWebApplication、ConditionalOnResource、ConditionalOnProperty、ConditionalOnExpression等条件注解,有兴趣的读者可以自行查看它们的底层处理逻辑。
# 各种条件注解的总结
# SpringBoot条件注解的激活机制
分析完了条件注解的执行逻辑之后,接下来的问题就是SpringBoot是如何让这些条件注解生效的?
SpringBoot使用ConditionEvaluator这个内部类完成条件注解的解析和判断。
在Spring容器的refresh过程中,只有跟解析或者注册bean有关系的类都会使用ConditionEvaluator完成条件注解的判断,这个过程中一些类不满足条件的话就会被skip。这些类比如有AnnotatedBeanDefinitionReader、ConfigurationClassBeanDefinitionReader、ConfigurationClassParse、ClassPathScanningCandidateComponentProvider等。
比如ConfigurationClassParser的构造函数会初始化内部属性conditionEvaluator:
public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory, ProblemReporter problemReporter, Environment environment, ResourceLoader resourceLoader, BeanNameGenerator componentScanBeanNameGenerator, BeanDefinitionRegistry registry) { this.metadataReaderFactory = metadataReaderFactory; this.problemReporter = problemReporter; this.environment = environment; this.resourceLoader = resourceLoader; this.registry = registry; this.componentScanParser = new ComponentScanAnnotationParser( resourceLoader, environment, componentScanBeanNameGenerator, registry); // 构造ConditionEvaluator用于处理条件注解 this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader);}
ConfigurationClassParser对每个配置类进行解析的时候都会使用ConditionEvaluator:
ConditionEvaluator的skip方法:
SpringBoot在条件注解的解析log记录在了ConditionEvaluationReport类中,可以通过BeanFactory获取(BeanFactory是有父子关系的;每个BeanFactory都存有一份ConditionEvaluationReport,互不相干):
ConditionEvaluationReport conditionEvaluationReport = beanFactory.getBean("autoConfigurationReport", ConditionEvaluationReport.class);Map<String, ConditionEvaluationReport.ConditionAndOutcomes> result = conditionEvaluationReport.getConditionAndOutcomesBySource();for(String key : result.keySet()) { ConditionEvaluationReport.ConditionAndOutcomes conditionAndOutcomes = result.get(key); Iterator iterator = conditionAndOutcomes.iterator(); while(iterator.hasNext()) { ConditionEvaluationReport.ConditionAndOutcome conditionAndOutcome = iterator.next(); System.out.println(key + " -- " + conditionAndOutcome.getCondition().getClass().getSimpleName() + " -- " + conditionAndOutcome.getOutcome()); }}
打印出条件注解下的类加载信息:
.......org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: freemarker.template.Configuration,org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryorg.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: groovy.text.markup.MarkupTemplateEngineorg.springframework.boot.autoconfigure.gson.GsonAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: com.google.gson.Gsonorg.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: org.h2.server.web.WebServletorg.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: org.springframework.hateoas.Resource,org.springframework.plugin.core.Pluginorg.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration -- OnClassCondition -- required @ConditionalOnClass classes not found: com.hazelcast.core.HazelcastInstance.......
来源:http://fangjian0423.github.io/2017/05/16/springboot-condition-annotation/
往期推荐
?
细细拆分了一下Spring容器的refresh过程,真难...
阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
要实现单点登录,难吗?
点击
@data注解不生效_面试官:你经常在SpringBoot中使用的条件注解底层是如何实现的?你了解过吗?...相关推荐
- .jar中没有主清单属性_面试官问:为什么SpringBoot的 jar 可以直接运行?
点击上方蓝色字体,选择"设为星标" 优质文章,及时送达 来源 | https://urlify.cn/uQvIna SpringBoot提供了一个插件spring-boot-mav ...
- redis查看key的过期时间_面试官:你在Redis中设置过带过期时间的Key吗?
点击上方小伟后端笔记关注公众号 每天阅读Java干货文章 熟悉Redis的同学应该知道,Redis的每个Key都可以设置一个过期时间,当达到过期时间的时候,这个key就会被自动删除. 在为key设置过 ...
- redis list设置过期时间_面试官:你在Redis中设置过带过期时间的Key吗?
点击上方小伟后端笔记关注公众号 每天阅读Java干货文章 熟悉Redis的同学应该知道,Redis的每个Key都可以设置一个过期时间,当达到过期时间的时候,这个key就会被自动删除. 在为key设置过 ...
- contentwindow无法搜索对象_面试官:讲一下Jvm中如何判断对象的生死?
但凡问到 JVM(Java 虚拟机)通常有 99% 的概率一定会问,在 JVM 中如何判断一个对象的生死状态? 判断对象的生死状态的算法有以下几个: 1.引用计数器算法 引用计算器判断对象是否存活的算 ...
- java 面试题 由浅入深_面试官由浅入深的面试套路
阅读文本大概需要3分钟. 从上图看来面试官面试是有套路的,一不小心就一直被套路. 0x01:Thread 面试官 :创建线程有哪几种方式? 应聘者 :继承Thread类.实现Runable接口.使用j ...
- 整天都说注解注解注解,你们了解注解吗?来自——面试官的灵魂拷问
注解 它可以用于创建文档,跟踪代码中的依赖性,甚至执行基本编译时检查.注解是以'@注解名'在代码中存在的,根据注解参数的个数,我们可以将注解分为:标记注解.单值注解.完整注解三类.它们都不会直接影响到 ...
- mysql编写完怎么执行_面试官:一条MySQL更新语句是如何执行的?
在面试中面试中如果被面试官问到在MySQL中一条更新语句是怎么执行的?,下面让我们来探究一下! 流程图 这是在网上找到的一张流程图,写的比较好,大家可以先看图,然后看详细阅读下面的各个步骤. 执行流程 ...
- 华为二面!!!面试官直接问我Java中到底什么是NIO?这不是直接送分题???
华为二面!!!面试官直接问我Java中到底什么是NIO?这不是直接送分题??? 什么是NIO 缓冲区(Buffer) 缓冲区类型 获取缓冲区 核心属性 核心方法 非直接缓冲区和直接缓冲区 非直接缓冲区 ...
- 面试官让我讲讲Java中的锁,我笑了
转载自 面试官让我讲讲Java中的锁,我笑了 在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁 ...
最新文章
- 小A与欧拉路(牛客-树的直径)
- 徒劳的对抗——如何做好极客的老婆(灵感来源于《你就是极客》)
- Python中文件读写之 w+ 与 r+ 到底有啥区别?
- 今奥无人机举证_【企业动态】今奥小飞无人机助力安徽省省级占补平衡核查与验收...
- Kinect开发笔记之五使用PowerShell控制Kinect
- 微服务SpringCloud中的负载均衡,你都会么?
- 区块链app源码_区块链app商城系统开发适用于哪些企业
- 关于忘记SYSKEY密码后清除密码操作
- PHP在Windows下安装配置第一步
- 国内破解站点大全! -by[http://blog.csdn.net/netxiaoyue]
- 企业级监控ZABBIX
- html 图片循环轮播,如何在Web端实现动画切换效果一致的无限循环图片轮播?
- 一篇文章告诉你大数据的重要性
- 现任明教教主CCNP Security SecureV1.0 第一天.3
- 命令模式实例与解析--实例一:电视机遥控器
- 【SOFA】SOFA框架+Win10+VS2019 配置
- Roboguide软件:机器人焊装夹具运动机构制作与仿真
- 在ubuntu下配置Defects4j
- 考虑未来,我转行了软件测试,入职薪资9K
- mysql支持表情输入_mysql支持emoji表情
热门文章
- 华南理工大学网络教育计算机答案,计算机应用基础--随堂练习2019春华南理工大学网络教育答案...
- 1加6投屏_今天说说投屏那点事,建议大家选购投屏器,要关注这些点
- Java黑皮书课后题第8章:*8.34(几何:最右下角的点)在计算几何中经常需要从一个点集中找到最右下角的点。编写一个测试程序,提示用户输入6个点的坐标,然后显示最右下角的点
- Java黑皮书课后题第8章:*8.21(中心城市)给定一组城市,中心城市是和其它所有城市具有最短距离的城市。编写一个程序,提示用户输入城市数目以及位置(坐标),找到中心城市以及与其他城市总距离
- C语言学习之将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,5,4,1. 要求改为1,4,5,6,8。
- 迅雷2014校园招聘笔试题
- Jmeter_简单的关联设置
- 用例设计:判定表驱动法
- Codeforces Round #401 (Div. 1) C(set+树状数组)
- python3-字典中的一些常用方法