@Conditional 原理
1,这里讲的是:org.springframework.context.annotation.Conditional
2,在springConfig文件里注册bean
@Conditional(ColorCondition.class)
@Bean
public Color red() {
Color red = new Color();
red.setName("红色");
red.setWeight("好靓");
return red;
}
3,ColorCondition 代码如下,我们在matches 方法内打一个断点
public class ColorCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String[] colors = context.getBeanFactory().getBeanNamesForType(Color.class);
for (String color : colors) {
if("red".equals(color)) {
return true;
}
}
return false;
}
}
4,断点如下

5,我们往前找,从入口类开始看

6,可以得出结论
①,是在执行AnnotationConfigApplicationContext#reflsh方法,调用invokeBeanFactoryPostProcessors,执行 BeanFactoryPostProcessorr的postProcessBeanDefinitionRegistry 方法
②,会加载bean的定义信息
③,会执行ConditionEvaluator#shouldSkip判断这个类是否应该被跳过
④,然后就会调用我们自定义的ColorCondition#matches方法
⑤,如果返回false,则不会注册对应bean到ioc容器中
@Conditional 原理的更多相关文章
- spring-注解驱动模式
spring web装配原理: /** * WebApplicationInitializer Spring MVC 提供接口. * * Spring中的web自动配置,也是可以, */ /** * ...
- 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 ...
- CSharpGL(30)用条件渲染(Conditional Rendering)来提升OpenGL的渲染效率
CSharpGL(30)用条件渲染(Conditional Rendering)来提升OpenGL的渲染效率 当场景中有比较复杂的模型时,条件渲染能够加速对复杂模型的渲染. 条件渲染(Conditio ...
- lighttpd与fastcgi+cgilua原理、代码分析与安装
原理 http://www.cnblogs.com/skynet/p/4173450.html 快速通用网关接口(Fast Common Gateway Interface/FastCGI)是通用网关 ...
- Spring Boot 启动原理分析
https://yq.aliyun.com/articles/6056 转 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启 ...
- Golang源码探索(三) GC的实现原理
Golang从1.5开始引入了三色GC, 经过多次改进, 当前的1.9版本的GC停顿时间已经可以做到极短. 停顿时间的减少意味着"最大响应时间"的缩短, 这也让go更适合编写网络服 ...
- spring-boot-2.0.3源码篇 - @Configuration、Condition与@Conditional
前言 开心一刻 一名劫匪慌忙中窜上了一辆车的后座,上车后发现主驾和副驾的一男一女疑惑地回头看着他,他立即拔出枪威胁到:“赶快开车,甩掉后面的警车,否则老子一枪崩了你!”,于是副驾上的男人转过脸对那女的 ...
- Springboot 系列(三)Spring Boot 自动配置原理
注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...
- SpringBoot系列二:SpringBoot自动配置原理
主程序类的注解 @SpringBootApplication 注解,它其实是个组合注解,源码如下: @Target({ElementType.TYPE}) @Retention(RetentionPo ...
随机推荐
- cadence单一原理图库的设计
- .NET平台常用的框架
转自:https://www.cnblogs.com/lhxsoft/p/8609089.html 分布式缓存框架: Microsoft Velocity:微软自家分布式缓存服务框架. Memcahe ...
- 【动态规划dp】青蛙的烦恼
青蛙的烦恼(frog) 原文:https://blog.csdn.net/xyc1719/article/details/79844952 [题目描述] 池塘中有 n 片荷叶恰好围成了一个凸多边形,有 ...
- csv impor export with mysql
server-side:SELECT id,tutorialId,tutorialName,ucreatelink,structureVersion FROM base_courseINTO OUTF ...
- poj1699
#include<iostream> #include<cstring> using namespace std; ][]; ],len[],addlen[][]; int m ...
- 001-JUnit之断言assert
一.简介以及pom JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活: 使用全新的断言语法:assertThat, ...
- 【记录tomcat报错解决办法】tomcat请求组件没有找到的问题
报错原因: An incompatible version 1.1.14 of APR based Apache Tomcat Native library is installed, while T ...
- TCP/IP 免费ARP
免费ARP Gratuitous ARP也称为免费ARP.Gratui ARP不同于一般的ARP请求,它并非期待得到IP对应的MAC地址,而是当主机启动的时候,将发送一个Gratuitous arp请 ...
- vue cli 3.0创建项目
.npm i -g @vue/cli .vue create my-project 此处有两个选择: 1.default (babel, eslint)默认套餐,提供babel和eslint支持 2. ...
- react脚手架搭建
1.npm install -g create-react-app 2.create-react-app react-demo 3.cd react-demo 4.npm start