Spring源码学习 ---Spring解析XML过程

Spring对XML配置文件的加载过程

参考:《Spring源码深度解析》

环境搭建需要:spring-core,spring-beans

一般最常见的使用BeanFactory获取Bean的方法如下(XmlBeanFactory现已被弃用)

public void testLoad(){
	BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("filePath"));
    ObjectBean oBean = (ObjectBean)beanFactory.getBean("beanName");
}

xml文件写为

<?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-4.3.xsd">

    <bean id="beanName" class="Beans.MyTestBean"/>

</beans>

其中XmlBeanFactory继承自DefaultListableBeanFactory(Bean加载的核心类)
DefaultListableBeanFactory(Bean加载的核心部分):综合下面的所有功能,主要是对bean注册后的处理
		extends: 
			AbstractAutowireCapableBeanFactory
				AbstractBeanFactory
				ConfigurableBeanFactory(1):提供配置Factory的各种方法
		implements:
			ConfigurableListableBeanFactory
				AutowireCapableBeanFactory:提供创建bean,自动注入,初始化及应用bean后的处理器
				ConfigurableBeanFactory(1)同
				ListableBeanFactory:根据各种条件获取bean的配置清单
			BeanDefinitionRegistry
				AliasRegistry:定义alias的简单增删改查等操作
			Serializable
第一步:读取Bean的配置文件(XML)

new ClassPathResource(“filename”);
ClassPathResource类的继承结构ClassPathResource(String filename)方法,通过传入文件名,将文件内容封装为InputStream,并且记录其他信息。Spring通过Resources接口抽象了Spring使用的内部资源:File,URL,ClassPath。将InputStream传入XmlBeanFactory()中。

第二步:开始处理InputStream

new XmlBeanFactory(Resource resource) 这里简化为传入一个 Resource对象

第三步:解析Document,注册Bean
总体流程:

总体执行流程

发布了30 篇原创文章 · 获赞 0 · 访问量 824

猜你喜欢

转载自blog.csdn.net/fantow/article/details/104756811