spring源码分析(三)XmlBeanDefinitionReader类

在XmlBeanFactory类中,XmlBeanFactory通过构造方法,以接近于下面这样的方式进行xml解析

new XmlBeanDefinitionReader(this).loadBeanDefinitions(resource);

所以我们就来讲一讲XmlBeanDefinitionReader类,照例,先看看官方是怎么说这个对象的

*用于XML Bean定义的Bean定义读取器。

*将实际的XML文档读取委托给实现

*{@link BeanDefinitionDocumentReader}接口的。

*

*<p>通常应用于

*{@link org.springframework.beans.factory.support.DefaultListableBeanFactory}

*或者{@link org.springframework.context.support.GenericApplicationContext}*

*<p>此类加载一个DOM文档并将BeanDefinitionDocumentReader应用于该文档。

*文档读取器将向给定的bean工厂注册每个bean定义,

*谈后者对

*{@link org.springframework.beans.factory.support.BeanDefinitionRegistry}接口。

*

*@作者Juergen Hoeller

*@作者Rob Harrop

*@作者Chris Beams

*@自20031126日起

*@请参阅setDocumentReaderClass

*@请参阅BeanDefinitionDocumentReader

*@请参阅DefaultBeanDefinitionDocumentReader

*@请参阅BeanDefinitionRegistry

*@请参见org.springframework.beans.factory.support.DefaultListableBeanFactory

*@请参见org.springframework.context.support.genericaplicationcontext

很明显,这就是我们想要寻找的xml解析类

查看构造方法

public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
		super(registry);
	}
super方法
protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
保证xml解析入口类不为空
		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		将入口类赋为成员变量
		this.registry = registry;

		// Determine ResourceLoader to use.
		判断xml入口类是否带有类加载器
		if (this.registry instanceof ResourceLoader) {
			this.resourceLoader = (ResourceLoader) this.registry;
		}
		else {
		否则使用默认类加载器
			this.resourceLoader = new PathMatchingResourcePatternResolver();
		}

		// Inherit Environment if possible
		如果可能,继承环境,本质上就是将可以复用的Java对象尝试进行复用,节省资源开销
		if (this.registry instanceof EnvironmentCapable) {
			this.environment = ((EnvironmentCapable) this.registry).getEnvironment();
		}
		else {
			this.environment = new StandardEnvironment();
		}
	}

接下来就是该类的关键方法,loadBeanDefinitions方法,该方法是解析的执行方法

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<EncodedResource>(4);
			this.resourcesCurrentlyBeingLoaded.set(currentResources);
		}
		若已存在该资源对象,抛出异常
		if (!currentResources.add(encodedResource)) {
			throw new BeanDefinitionStoreException(
					"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
		}
		try {
		获取资源对象的io连接
			InputStream inputStream = encodedResource.getResource().getInputStream();
			try {
			将io连接交给包装类处理
				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();
			}
		}
	}

进入doLoadBeanDefinitions方法

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
			throws BeanDefinitionStoreException {
		try {
		这两个是关键方法,其他的只不过是为了给与用户准确的报错提示
			Document doc = doLoadDocument(inputSource, resource);
			return registerBeanDefinitions(doc, resource);
		}
		catch (BeanDefinitionStoreException ex) {
			throw ex;
		}
		catch (SAXParseException ex) {
			throw new XmlBeanDefinitionStoreException(resource.getDescription(),
					"Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
		}
		catch (SAXException ex) {
			throw new XmlBeanDefinitionStoreException(resource.getDescription(),
					"XML document from " + resource + " is invalid", ex);
		}
		catch (ParserConfigurationException ex) {
			throw new BeanDefinitionStoreException(resource.getDescription(),
					"Parser configuration exception parsing XML from " + resource, ex);
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException(resource.getDescription(),
					"IOException parsing XML document from " + resource, ex);
		}
		catch (Throwable ex) {
			throw new BeanDefinitionStoreException(resource.getDescription(),
					"Unexpected exception parsing XML document from " + resource, ex);
		}
	}

进入doLoadDocument方法

五个参数分别为,io包装对象,解析器对象,记录器对象(记录错误的xml解析,方便查找错误),解析模式(
XSD或DTD),解析器是否应该解析xml命名空间
protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
		return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
				getValidationModeForResource(resource), isNamespaceAware());
	}
loadDocument方法通过上面的五个参数获取解析io的解析对象
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
			ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
根据解析模式和是否解析命名空间生成解析工程
		DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
		if (logger.isDebugEnabled()) {
			logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
		}
		根据解析工厂和解析器,记录器生成解析对象
		DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
		开始解析
		return builder.parse(inputSource);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值