Spring配置文件解析(二)BeanDefinitionReader源码分析

1 概述

这一篇文章的作用主要是接着上篇文章(Spring配置文件解析(一)ClassPathXmlApplicationContext源码分析)继续分析在Spring中配置文件的加载和解析,但是在分析之前,我们有必要对BeanDefinitionReader家族进行一个简单地介绍。
这个BeanDefinitionReader接口是bean定义读取的基础接口,它的函数根据不同的资源获取方式进行了多个重载。
这里除了loadBeanDefinitions函数,我们来看看其余的函数。

2 函数

(1)BeanDefinitionRegistry getRegistry();
返回bean定义注册接口的对象,这个BeanDefinitionRegistry接口其实就提供了访问BeanDefinition的功能。
(2)ResourceLoader getResourceLoader();
返回资源加载器。
(3)BeanNameGenerator getBeanNameGenerator();
返回BeanNameGenerator接口对于匿名的bean。

3 派生类

(1)AbstractBeanDefinitionReader
bean定义读取的基础抽象类,实现了BeanDefinitionReader接口。
(2)XmlBeanDefinitionReader
用于处理XML文档中的bean定义的接口,这个接口将XML文档的读取操作委托给BeanDefinitionDocumentReader接口的实现类。
至于其余的派生类我们这里就不继续分析了,可以自行参考Spring源码学习。

4 配置文件解析分析

从这里开始我们接着上一篇文章Spring配置文件解析(一)ClassPathXmlApplicationContext源码分析对配置文件解析的分析,来分析在BeanDefinitionReader中做了什么。通过上一篇文章的学习,我们知道最终会调用AbstractBeanDefinitionReader的public int loadBeanDefinitions(String... locations)函数来实现配置文件的解析和加载。该函数的源码如下:

public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
	Assert.notNull(locations, "Location array must not be null");
	int counter = 0;
	
	//遍历配置文件资源的地址以加载多个资源文件
	for (String location : locations) {
		counter += loadBeanDefinitions(location);
	}
	return counter;
}

所以下面我们来看一看针对单个资源的解析过程。

public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
	return loadBeanDefinitions(location, null);
}

这个函数几乎什么都没有做,直接去调了下面这个函数。

/**
 * 从指定的资源位置加载bean定义。
 * 其实这个函数的作用就是获取到最终的Resource对象
 * 
 */
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {

    //获取资源加载器
	ResourceLoader resourceLoader = getResourceLoader();
	if (resourceLoader == null) {
		throw new BeanDefinitionStoreException(
				"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
	}

	//判断资源地址类型:是否是“classpath*:型的。
	if (resourceLoader instanceof ResourcePatternResolver) {
		// Resource pattern matching available.
		try {
		
		    //获取到指定资源
			Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
			int loadCount = loadBeanDefinitions(resources);
			if (actualResources != null) {
				for (Resource resource : resources) {
					actualResources.add(resource);
				}
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
			}
			return loadCount;
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException(
					"Could not resolve bean definition resource pattern [" + location + "]", ex);
		}
	}
	else {
		// Can only load single resources by absolute URL.
		Resource resource = resourceLoader.getResource(location);
		int loadCount = loadBeanDefinitions(resource);
		if (actualResources != null) {
			actualResources.add(resource);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
		}
		return loadCount;
	}
}

在上面的函数中,最终获取到Resource,会调用XmlBeanDefinitionReader的public int loadBeanDefinitions(Resource resource)函数。

@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
	return loadBeanDefinitions(new EncodedResource(resource));
}

在此,我们将Resource对象封装成EncodedResource对象,然后去调用XmlBeanDefinitionReader的public int loadBeanDefinitions(EncodedResource encodedResource)函数。
这里针对EncodedResource简单说明一下。
EncodedResource其实就是对资源、编码、字符集的封装。

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
	Assert.notNull(encodedResource, "EncodedResource must not be null");
	if (logger.isInfoEnabled()) {
		logger.info("Loading XML bean definitions from " + encodedResource.getResource());
	}

	Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
	if (currentResources == null) {
		currentResources = new HashSet<>(4);
		this.resourcesCurrentlyBeingLoaded.set(currentResources);
	}
	if (!currentResources.add(encodedResource)) {
		throw new BeanDefinitionStoreException(
				"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
	}
	try {
	
	    //从XML文件中获取流
		InputStream inputStream = encodedResource.getResource().getInputStream();
		try {
			InputSource inputSource = new InputSource(inputStream);
			if (encodedResource.getEncoding() != null) {
				inputSource.setEncoding(encodedResource.getEncoding());
			}
			
			//解析文件流
			return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
		}
		finally {
			inputStream.close();
		}
	}
	catch (IOException ex) {
		throw new BeanDefinitionStoreException(
				"IOException parsing XML document from " + encodedResource.getResource(), ex);
	}
	finally {
		currentResources.remove(encodedResource);
		if (currentResources.isEmpty()) {
			this.resourcesCurrentlyBeingLoaded.remove();
		}
	}
}

上面的函数其实最终的作用就是将EncodeResource解析成文件流InputSource。然后来调用XmlBeanDefinitionReader类的protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)函数进行处理。

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
	throws BeanDefinitionStoreException {
	...
	
	//将文件流解析成Document对象
	Document doc = doLoadDocument(inputSource, resource);
	
	//注册bean定义
	return registerBeanDefinitions(doc, resource);
	...
}

在注册bean定义的函数registerBeanDefinitions中,会创建BeanDefinitionDocumentReader来对Document进行解析,对Spring配置文件中的元素进行解析处理。

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
	//创建Document解析处理器
	BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
	int countBefore = getRegistry().getBeanDefinitionCount();
	//在BeanDefinitionDocumentReader中解析xml中配置的元素
	documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
	return getRegistry().getBeanDefinitionCount() - countBefore;
}

至此,我们简单总结一下,BeanDefinitionReader的作用其实就是将XML配置文件解析成Document对象,接下来会有BeanDefinitionDocumentReader来对Document进行解析。
后面我们将继续分析BeanDefinitionDocumentReader的操作,欢迎交流。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值