当前位置:网站首页>xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
2022-07-02 18:14:00 【轻尘×】
AutowiredAnnotationBeanPostProcessor是实现@Autowired的关键,它本身是在何时添加到sping容器中的?
xml方式
在使用xml方式开发的过程中,我们程序入口一般是ClassPathXmlApplicationContext
@Test
public void test() throws Exception{
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-test.xml");
Student student = (Student)applicationContext.getBean("student");
System.out.println(student.getUsername()+" "+student.getPassword());
}创建ClassPathXmlApplicationContext时,会调用this构造函数

在构造函数中调用父类构造函数,完成一些属性的加载,以及配置文件路径的解析工作,然后进入正题——refresh()

在核心的refresh()方法中,有一个步骤是创建BeanFactory

创建BeanFactory后,它顺带把我们的xml配置文件也给解析了

创建 BeanFactory,然后 loadBeanDefinitions
loadBeanDefinitions的过程比较复杂,有兴趣的可以去逐行degbug源码,这里就贴几个关键步骤

几个相同名称的loadBeanDefinitions调来调去,最好调到了真正干活的doLoadBeanDefinitions

loadBeanDefinitions并不是这次关注的焦点,doLoadBeanDefinitions的细节也就不用深究了,重要的是在doLoadBeanDefinitions完成后,还只是得到了一个Document得对象,还不是我们要的bean定义信息,即BeanDefinition,所以继续进行了一个叫registerBeanDefinitions的步骤

在这个registerBeanDefinitions的过程中,九曲十八弯

也是调do开头的方法进行真正的干活
开始解析前面的Document了

解析的标签分两种类型,一种是DefaultElement,一种是CustomElement

扯个题外话,什么是DefaultElement,这四个就是了,分别是import、alias、bean、beans,换句话说,除了这四个,其他的都是CustomElement

在我的测试环境中,第一个标签是context,自然就是走parseCustomElement

那就继续解析吧

调到NamespaceHandlerSupport的parse方法

再往下走的又是ComponentScanBeanDefinitionParser的parse方法,终于到头了,把标签解析并封装成了BeanDefinitionHolder,而后又有一个动作registerComponents

就是这个不起眼的小动作,registerComponents,里面蕴含玄机

在这里面执行了一个AnnotationConfigUtils.registerAnnotationConfigProcessors,就是在这里完成了AutowiredAnnotationBeanPostProcessor的注册
总而言之,简而言之,就是在创建容器后,loadBeanDefinitions时注册的。
注解方式
同样整一个测试方法,即这次new的是AnnotationConfigApplicationContext 而不是上面的ClassPathXmlApplicationContext
@Test
public void test() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("com.zubus.bean");
BeforeInstantiation bean = ac.getBean(BeforeInstantiation.class);
bean.doSomeThing();
}走的是不同的路线,进this()

里面会new一个AnnotatedBeanDefinitionReader


new AnnotatedBeanDefinitionReader()的过程中有个熟悉的身影
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
是不是和上面的xml解析后的registerComponents里的一模一样

没错,正是在下
至此,两种开发模式下的AutowiredAnnotationBeanPostProcessor注册时机我们已经掌握了
这里仅仅是注册,后续就是再分析它是在哪没创建成对象的
再后续就是分析它是在哪里被调用执行了
请看下回分解~
边栏推荐
- PHP非对称加密方法私钥及公钥加密解密的方法
- R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
- [100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
- "Patient's family, please come here" reading notes
- 聊聊电商系统中红包活动设计
- QT中的QPropertyAnimation使用和toast案列
- 守望先锋世界观架构 ——(一款好的游戏是怎么来的)
- Golang并发编程——goroutine、channel、sync
- 【JVM调优实战100例】01——JVM的介绍与程序计数器
- 【pytorch学习笔记】Tensor
猜你喜欢

医院在线问诊源码 医院视频问诊源码 医院小程序源码

Hospital online inquiry source code hospital video inquiry source code hospital applet source code

MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

论文导读 | 关于将预训练语言模型作为知识库的分析与批评

Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比

Talk about the design of red envelope activities in e-commerce system

守望先锋世界观架构 ——(一款好的游戏是怎么来的)

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5

Codeworks 5 questions per day (1700 average) - day 4
随机推荐
C文件输入操作
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
R language ggplot2 visualization: gganimate package creates dynamic histogram animation (GIF) and uses transition_ The States function displays a histogram step by step along a given dimension in the
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
C的内存管理
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
2022 compilation principle final examination recall Edition
虚拟机初始化脚本, 虚拟机相互免秘钥
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
Use cheat engine to modify money, life and stars in Kingdom rush
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Golang concurrent programming goroutine, channel, sync
Yolov3 trains its own data set to generate train txt
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
Novice must see, click two buttons to switch to different content
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
PHP-Parser羽毛球预约小程序开发require线上系统
2022软件工程期末考试 回忆版
