当前位置:网站首页>注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
2022-07-02 18:14:00 【轻尘×】
注解方式
写一个测试方法,new的是AnnotationConfigApplicationContext ,指定basePackages
@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);
继续跟进

正是在这个方法内,把AutowiredAnnotationBeanPostProcessor的注册到了Set<BeanDefinitionHolder>中

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的注册
比注解方式复杂太多,隐藏的太深了,总而言之,简而言之,xml方式就是在创建容器后,loadBeanDefinitions时注册的。
至此,两种开发模式下的AutowiredAnnotationBeanPostProcessor注册时机我们已经掌握了
这里仅仅是注册,后续就是再分析它是在哪没创建成对象的
再后续就是分析它是在哪里被调用执行了
请看下回分解~
边栏推荐
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
- Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
- golang:[]byte转string
- Novice must see, click two buttons to switch to different content
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
- Emmet基础语法
- 电脑使用哪个录制视频软件比较好
- Markdown basic grammar
- Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
- 预处理和预处理宏
猜你喜欢
![[fluent] dart data type (VaR data type | object data type)](/img/1b/fe2529af5f6663fad1fb7861f14ab5.jpg)
[fluent] dart data type (VaR data type | object data type)
![[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter](/img/c4/3bba96fda92328704c2ddd929dcdf6.png)
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter

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

Have you stepped on the nine common pits in the e-commerce system?

消息队列消息丢失和消息重复发送的处理策略

Juypter notebook modify the default open folder and default browser

Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
随机推荐
PHP-Parser羽毛球预约小程序开发require线上系统
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
NPOI导出Excel2007
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
mysql备份后缀是什么_mysql备份还原
R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
The difference between interceptor and filter
Crypto usage in nodejs
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
Markdown基础语法
Use cheat engine to modify money, life and stars in Kingdom rush
新手必看,點擊兩個按鈕切換至不同的內容
codeforces每日5题(均1700)-第四天
Emmet基础语法
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
消息队列消息丢失和消息重复发送的处理策略
C file input operation
2022编译原理期末考试 回忆版
Machine learning notes - time series prediction research: monthly sales of French champagne
【测试开发】一文带你了解什么是软件测试
