当前位置:网站首页>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注册时机我们已经掌握了
这里仅仅是注册,后续就是再分析它是在哪没创建成对象的
再后续就是分析它是在哪里被调用执行了
请看下回分解~
边栏推荐
- Golang并发编程——goroutine、channel、sync
- 【测试开发】一文带你了解什么是软件测试
- ICDE 2023|TKDE Poster Session(CFP)
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
- In pytorch function__ call__ And forward functions
- R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
- Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
猜你喜欢

全志A33使用主线U-Boot

Novice must see, click two buttons to switch to different content

高频面试题

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

How can retail enterprises open the second growth curve under the full link digital transformation
![[paper reading] Ca net: leveraging contextual features for lung cancer prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[paper reading] Ca net: leveraging contextual features for lung cancer prediction

Juypter notebook modify the default open folder and default browser

聊聊电商系统中红包活动设计

yolov3 训练自己的数据集之生成train.txt

Stm32g0 USB DFU upgrade verification error -2
随机推荐
Excel finds the same value in a column, deletes the row or replaces it with a blank value
Markdown basic grammar
Develop fixed asset management system, what voice is used to develop fixed asset management system
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
2022编译原理期末考试 回忆版
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
Juypter notebook modify the default open folder and default browser
2022软件工程期末考试 回忆版
Learn the knowledge points of eight part essay ~ ~ 1
codeforces每日5题(均1700)-第四天
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Yolov3 trains its own data set to generate train txt
9D电影是怎样的?(+维度空间常识)
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
页面标题组件
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
Thread application instance
