闲聊spring源码二(xml方式如何注册BeanDefinition)

本文详细介绍了Spring6.0.12版本中如何通过XML文件配置BeanDefinition,涉及的关键类和方法包括ClassPathXmlApplicationContext、XmlBeanDefinitionReader等,展示了从加载XML文件到注册Bean的完整过程。
摘要由CSDN通过智能技术生成

概述

spring 版本6.0.12,阐述spring是如何通过xml文件配置的方式向容器中注册BeanDefinition

spring 源码准备
在这里插入图片描述

相关链接

重点类与方法

正文之前,先将重要的类与方法标注,通过下面的详细可以快速定位到代码地点,打上断点进行调试

# 关键调试断点
org.springframework.context.support.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)
org.springframework.context.support.AbstractApplicationContext#refresh
org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory
org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.support.DefaultListableBeanFactory)
# 以下是非常重要的点
org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.support.EncodedResource)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader#doLoadBeanDefinitions
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#doRegisterBeanDefinitions
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#parseBeanDefinitions
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#parseDefaultElement
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#processBeanDefinition

源码调试

在这里插入图片描述
上图中相关代码依次展示

package org.springframework.my;

import org.springframework.stereotype.Component;

@Component
public class TestBean {
	public void print() {
		System.out.println("test bean method ...");
		System.out.println("spring源码环境构建完成");
	}
}
package org.springframework.test.my;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.my.TestBean;

public class IOCTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		TestBean bean = context.getBean(TestBean.class);
		bean.print();
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="testBean" class="org.springframework.my.TestBean"/>
</beans>

先概括一下流程:1.加载xml文件,2.将xml转化为document对象3.将document转化成一个BeanDefinition

  1. 加载xml文件涉及到了spring 资源加载方式
  2. xml转化成document (这一块可以跳过,不重要)
  3. doc对象转化 (涉及bean空间的定义,后续有机会补充)

上面3点,看的时候可以选择性跳过

以下图片顺序及调试顺序(图片上有中文注释)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

得到

通过重点类与方法调试点,可以快速打断点,spring xml方式注册bean的脉络就清楚了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流月up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值