java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring的BeanDefinitionReader

Spring配置文件解析之BeanDefinitionReader详解

作者:军伟@

这篇文章主要介绍了Spring配置文件解析之BeanDefinitionReader详解,ApplicationContext.xml配置文件解析成Document对象,真正对xml中元素解析的类是在BeanDefinitionDocumentReader的实现类中来完成的,需要的朋友可以参考下

BeanDefinitionReader

Spring配置文件的解析是通过BeanDefinitionReader来实现的,其实了解BeanDefinitionReader实现的机制就会发现,其只是将ApplicationContext.xml配置文件解析成Document对象,真正对xml中元素解析的类是在BeanDefinitionDocumentReader的实现类中来完成的,接下来我们也会对它进行介绍。

对ApplicationContext.xml中开始解析的方法是loadBeanDefinitions

AbstractBeanDefinitionReader的loadBeanDefinitions中会对ApplicationContext.xml进行解析

@Override
	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;
	}
@Override
	public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
		return loadBeanDefinitions(location, null);
	}

loadBeanDefinitions中会根据文件地址来进行解析操作:

public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
		//判断资源地址类型
		if (resourceLoader instanceof ResourcePatternResolver) {
			// Resource pattern matching available.
			try {
				Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
				//在子类的loadBeanDefinitions进行解析操作
				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);
			//在子类的loadBeanDefinitions进行解析操作
			int loadCount = loadBeanDefinitions(resource);
			if (actualResources != null) {
				actualResources.add(resource);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
			}
			return loadCount;
		}
	}

loadBeanDefinitions是在子类XmlBeanDefinitionReader中实现

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

doLoadBeanDefinitions会将文件地址解析为数据流,然后解析数据流

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
		
		..........
		
		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();
			}
		}
		.......
	}

doLoadBeanDefinitions中会将文件流解析成Document对象

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
			throws BeanDefinitionStoreException {
		try {
			Document doc = doLoadDocument(inputSource, resource);
			return registerBeanDefinitions(doc, resource);
		}
		.....
	}

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所做的处理操作是将配置的ApplicationContext.xml解析成为Document对象,接下来会有 BeanDefinitionDocumentReader来对Document进行解析。

到此这篇关于Spring配置文件解析之BeanDefinitionReader详解的文章就介绍到这了,更多相关Spring的BeanDefinitionReader内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文