spring解析import标签过程分析

写在前面

本篇在这篇文章基础上继续分析。本篇主要分析的是import标签导入配置文件的过程。

1:作用

解耦配置文件,减少配置文件编写和后期维护的的复杂度。

2:测试代码

为了方便调试再贴下测试代码:

@Test
public void testBeanDefinitionLoad() {
    
    
    // 定义资源
    ClassPathResource classPathResource = new ClassPathResource("testbeandefinition.xml");
    // 定义IOC容器
    DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
    // 定义bean定义读取器
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);
    // 通过bean定义读取器从资源中读取bean定义
    int i = xmlBeanDefinitionReader.loadBeanDefinitions(classPathResource);
    System.out.println("bean定义的个数是:" + i);
}

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.xsd">
    <bean id="testBeanDefinitionBean" class="yudaosourcecode.spring.TestBeanDefinitionBean"/>
    <bean id="testBeanDefinitionBean1" class="yudaosourcecode.spring.TestBeanDefinitionBean"/>
    <import resource="needimported.xml"/>
</beans>

被引入配置文件needimported.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.xsd">
    <bean id="testBeanDefinitionBean2" class="yudaosourcecode.spring.TestBeanDefinitionBean"/>
</beans>

然后在org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#parseDefaultElement处打如下断点,就可以开始调试了:
在这里插入图片描述

3:importBeanDefinitionResource

源码:

org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#importBeanDefinitionResource
protected void importBeanDefinitionResource(Element ele) {
    
    
	// 获取import标签的resource属性的值,即需要引入的配置文件的地址
	String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
	// 如果是resource值为空,直接error,并return
	if (!StringUtils.hasText(location)) {
    
    
		getReaderContext().error("Resource location must not be empty", ele);
		return;
	}
	// <2021-03-01 10:54>
	// 解析系统环境变量
	location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);
	// 最终需要加载的资源的集合
	Set<Resource> actualResources = new LinkedHashSet<>(4);
	// 是相对路径还是绝对路径?
	boolean absoluteLocation = false;
	try {
    
    
		// 1:classpath:,classpath*:是绝对路径
		// 2:能够构造url的是绝对路径,如file:/xxxx,http://,jar:// 等资源访问协议定义的路径
		absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
	}
	catch (URISyntaxException ex) {
    
    
		...snip...
	}

	// 如果是绝对路径,classpath:,classpath*:,file:/,http:等
	// 这里需要区分是相对路径还是绝对路径的原因是,递归加载
	// import资源使用的方式和API会有所不同
	if (absoluteLocation) {
    
    
		try {
    
    
			// 递归导入,注意这里调用的是loadBeanDefinition的另一个重载版本了,不过殊途同归
			int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);
			if (logger.isTraceEnabled()) {
    
    
				logger.trace("Imported " + importCount + " bean definitions from URL location [" + location + "]");
			}
		}
		catch (BeanDefinitionStoreException ex) {
    
    
			getReaderContext().error(
					"Failed to import bean definitions from URL location [" + location + "]", ele, ex);
		}
	}
	// 如果是相对路径,非以classpath:,classpath*:,http://.file:/等开头的路径
	// 如果不是绝对路径则默认就是相对于当前资源的相对路径
	else {
    
    
		try {
    
    
			int importCount;
			// 根据当前资源(当前解析的import标签所在资源)的相对路径创建需要import的资源
			Resource relativeResource = getReaderContext().getResource().createRelative(location);
			// 需要import的相对路径资源存在
			if (relativeResource.exists()) {
    
    
				// 递归调用
				importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);
				// 添加资源
				actualResources.add(relativeResource);
			}
			// 需要import的相对路径资源不存在,理论上相对不存在,这种方式也不会存在,不知道为啥还要执行后续操作
			else {
    
    
				// 获取当前资源基础路径,如file:/E:/workspace-idea/java-life/target/classes/testbeandefinition.xml
				String baseLocation = getReaderContext().getResource().getURL().toString();
				// <2021-03-01 11:32>
				importCount = getReaderContext().getReader().loadBeanDefinitions(
						StringUtils.applyRelativePath(baseLocation, location), actualResources);
			}
			if (logger.isTraceEnabled()) {
    
    
				logger.trace("Imported " + importCount + " bean definitions from relative location [" + location + "]");
			}
		}
		catch (IOException ex) {
    
    
			getReaderContext().error("Failed to resolve current resource location", ele, ex);
		}
		catch (BeanDefinitionStoreException ex) {
    
    
			getReaderContext().error(
					"Failed to import bean definitions from relative location [" + location + "]", ele, ex);
		}
	}
	// 加载到的资源转成数据,并触发导入已经处理事件
	Resource[] actResArray = actualResources.toArray(new Resource[0]);
	getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
}

<2021-03-01 10:54>是替换环境变量,例如有如下的环境变量:

C:\Users\Administrator>set
ALLUSERSPROFILE=C:\ProgramData
...snip...

然后修改xml为<import resource="${ALLUSERSPROFILE}/needimported.xml"/>,在该处debug,获取的值如下:
在这里插入图片描述
可以看到已经将环境变量的实际值进行了替换。
<2021-03-01 11:32>处是根据基础绝对路径应用相对路径,获取相对路径的绝对路径,如基础绝对路径是file:/E:/workspace-idea/java-life/target/classes/testbeandefinition.xml,相对路径是needimported1.xml,则结果就是file:/E:/workspace-idea/java-life/target/classes/needimported1.xml
接着我们来看通过这里看下bean标签的解析过程,因为最终都是要解析bean标签的。

猜你喜欢

转载自blog.csdn.net/wang0907/article/details/114252284
今日推荐