当前位置:网站首页>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注册时机我们已经掌握了
这里仅仅是注册,后续就是再分析它是在哪没创建成对象的
再后续就是分析它是在哪里被调用执行了
请看下回分解~
边栏推荐
- R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
- 高频面试题
- 2022 compilation principle final examination recall Edition
- Progress-进度条
- What is 9D movie like? (+ common sense of dimension space)
- PyTorch函数中的__call__和forward函数
- SIFT特征点提取「建议收藏」
- metric_ Logger urination
- High frequency interview questions
- 日期工具类(不定时更新)
猜你喜欢

What is 9D movie like? (+ common sense of dimension space)

juypter notebook 修改默认打开文件夹以及默认浏览器
![[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction

【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
![[daily question] the next day](/img/8a/18329bd9b4a3a4445c8fbbc1ce562b.png)
[daily question] the next day

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

How to play when you travel to Bangkok for the first time? Please keep this money saving strategy

开发固定资产管理系统,开发固定资产管理系统用什么语音

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

ICDE 2023|TKDE Poster Session(CFP)
随机推荐
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
Thread application instance
Stm32g0 USB DFU upgrade verification error -2
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
[daily question] the next day
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
Processing strategy of message queue message loss and repeated message sending
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
PyTorch函数中的__call__和forward函数
Crypto usage in nodejs
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
《病人家属,请来一下》读书笔记
拦截器与过滤器的区别
How can retail enterprises open the second growth curve under the full link digital transformation
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
GMapping代码解析[通俗易懂]
Emmet basic syntax
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
Markdown basic grammar
mysql备份后缀是什么_mysql备份还原
