微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring源码-解析xml文件成Document对象

一、入口

    protected ConfigurableListablebeanfactory obtainFreshbeanfactory() {
	// 初始化beanfactory,并进行XML文件读取,并将得到的beanfactory记录在当前实体的属性中
	refreshbeanfactory();
	// 返回当前实体的beanfactory属性
	return getbeanfactory();
}

二、refreshbeanfactory

    @Override
protected final void refreshbeanfactory() throws BeansException {
	// 如果存在beanfactory,则销毁beanfactory
	if (hasbeanfactory()) {
		destroyBeans();
		closebeanfactory();
	}
	try {
		// 创建DefaultListablebeanfactory对象
		DefaultListablebeanfactory beanfactory = createbeanfactory();
		// 为了序列化指定id,可以从id反序列化到beanfactory对象
		beanfactory.setSerializationId(getId());
		// 定制beanfactory,设置相关属性包括是否允许覆盖同名称的不同定义的对象以及循环依赖
		customizebeanfactory(beanfactory);
		// 初始化documentReader,并进行XML文件读取及解析,认命名空间的解析,自定义标签的解析
		loadBeanDeFinitions(beanfactory);
		this.beanfactory = beanfactory;
	}
	catch (IOException ex) {
		throw new ApplicationContextException("I/O error parsing bean deFinition source for " + getdisplayName(), ex);
	}
}

三、createbeanfactory

    **
 * 构造方法,忽略BeanNameAware,beanfactoryAware,BeanClassLoaderAware的依赖
 *
 */
public AbstractAutowireCapablebeanfactory() {
	super();
	// 忽略要依赖的接口
	ignoreDependencyInterface(BeanNameAware.class);
	ignoreDependencyInterface(beanfactoryAware.class);
	ignoreDependencyInterface(BeanClassLoaderAware.class);
}

四、customizebeanfactory

    protected void customizebeanfactory(DefaultListablebeanfactory beanfactory) {
	// 如果属性allowBeanDeFinitionOverriding不为空,设置给beanfactory对象相应属性,是否允许覆盖同名称的不同定义的对象
	if (this.allowBeanDeFinitionOverriding != null) {
		beanfactory.setAllowBeanDeFinitionOverriding(this.allowBeanDeFinitionOverriding);
	}
	// 如果属性allowCircularReferences不为空,设置给beanfactory对象相应属性,是否允许bean之间存在循环依赖
	if (this.allowCircularReferences != null) {
		beanfactory.setAllowCircularReferences(this.allowCircularReferences);
	}
}

五、loadBeanDeFinitions

    @Override
protected void loadBeanDeFinitions(DefaultListablebeanfactory beanfactory) throws BeansException, IOException {
	// 创建一个xml的beanDeFinitionReader,并通过回调设置到beanfactory中
	XmlBeanDeFinitionReader beanDeFinitionReader = new XmlBeanDeFinitionReader(beanfactory);

	
	// 给reader对象设置环境对象
	beanDeFinitionReader.setEnvironment(this.getEnvironment());
	beanDeFinitionReader.setResourceLoader(this);
	beanDeFinitionReader.setEntityResolver(new ResourceEntityResolver(this));

	//  初始化beanDeFinitionReader对象,此处设置配置文件是否要进行验证
	initBeanDeFinitionReader(beanDeFinitionReader);
	// 开始完成beanDeFinition的加载
	loadBeanDeFinitions(beanDeFinitionReader);
}

六、loadBeanDeFinitions

     protected void loadBeanDeFinitions(XmlBeanDeFinitionReader reader) throws BeansException, IOException {
	// 以Resource的方式获得配置文件的资源位置
	Resource[] configResources = getConfigResources();
	if (configResources != null) {
		reader.loadBeanDeFinitions(configResources);
	}
	// 以String的形式获得配置文件的位置
	String[] configLocations = getConfigLocations();
	if (configLocations != null) {
		reader.loadBeanDeFinitions(configLocations);
	}
}

七、loadBeanDeFinitions

    @Override
public int loadBeanDeFinitions(String... locations) throws BeanDeFinitionStoreException {
	Assert.notNull(locations, "Location array must not be null");
	int count = 0;
	for (String location : locations) {
		count += loadBeanDeFinitions(location);
	}
	return count;
}

    @Override
public int loadBeanDeFinitions(String location) throws BeanDeFinitionStoreException {
	return loadBeanDeFinitions(location, null);
}

八、loadBeanDeFinitions

    public int loadBeanDeFinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDeFinitionStoreException {
	//此处获取resourceLoader对象
	ResourceLoader resourceLoader = getResourceLoader();
	if (resourceLoader == null) {
		throw new BeanDeFinitionStoreException(
				"Cannot load bean deFinitions from location [" + location + "]: no ResourceLoader available");
	}

	if (resourceLoader instanceof ResourcePatternResolver) {
		// Resource pattern matching available.
		try {
			// 调用DefaultResourceLoader的getResource完成具体的Resource定位
			Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
			int count = loadBeanDeFinitions(resources);
			if (actualResources != null) {
				Collections.addAll(actualResources, resources);
			}
			if (logger.isTraceEnabled()) {
				logger.trace("Loaded " + count + " bean deFinitions from location pattern [" + location + "]");
			}
			return count;
		}
		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 count = loadBeanDeFinitions(resource);
		if (actualResources != null) {
			actualResources.add(resource);
		}
		if (logger.isTraceEnabled()) {
			logger.trace("Loaded " + count + " bean deFinitions from location [" + location + "]");
		}
		return count;
	}
}


    @Override
public int loadBeanDeFinitions(Resource resource) throws BeanDeFinitionStoreException {
	return loadBeanDeFinitions(new EncodedResource(resource));
}

九、loadBeanDeFinitions
获取Resource对象中的文件流对象,然后转为InputSource,并设置编码。InputSource是jdk中的sax中xml文件解析对象。提供的一种输入规范。之后执行doLoadBeanDeFinitions进行XML的解析。

    public int loadBeanDeFinitions(EncodedResource encodedResource) throws BeanDeFinitionStoreException {
	Assert.notNull(encodedResource, "EncodedResource must not be null");
	if (logger.isTraceEnabled()) {
		logger.trace("Loading XML bean deFinitions from " + encodedResource);
	}

	// 通过属性来记录已经加载的资源
	Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();

	if (!currentResources.add(encodedResource)) {
		throw new BeanDeFinitionStoreException(
				"Detected cyclic loading of " + encodedResource + " - check your import deFinitions!");
	}

	// 从encodedResource中获取已经封装的Resource对象并再次从Resource中获取其中的inputStream
	try (InputStream inputStream = encodedResource.getResource().getInputStream()) {
		InputSource inputSource = new InputSource(inputStream);
		if (encodedResource.getEncoding() != null) {
			inputSource.setEncoding(encodedResource.getEncoding());
		}
		// 逻辑处理的核心步骤
		return doLoadBeanDeFinitions(inputSource, encodedResource.getResource());
	}
	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 {
		// 此处获取xml文件的document对象,这个解析过程是由documentLoader完成的,从String[] -string-Resource[]- resource,最终开始将resource读取成一个document文档,根据文档的节点信息封装成一个个的BeanDeFinition对象
		Document doc = doLoadDocument(inputSource, resource);
		int count = registerBeanDeFinitions(doc, resource);
		if (logger.isDebugEnabled()) {
			logger.debug("Loaded " + count + " bean deFinitions from " + resource);
		}
		return count;
	}
	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);
	}
}


    public int registerBeanDeFinitions(Document doc, Resource resource) throws BeanDeFinitionStoreException {
	// 实例化创建documentReader对象,documentReader对xml的beanDeFinition进行解析
	BeanDeFinitionDocumentReader documentReader = createBeanDeFinitionDocumentReader();
	int countBefore = getRegistry().getBeanDeFinitionCount();
	// 完成具体的解析过程
	documentReader.registerBeanDeFinitions(doc, createReaderContext(resource));
	return getRegistry().getBeanDeFinitionCount() - countBefore;
}


    @Override
public void registerBeanDeFinitions(Document doc, XmlReaderContext readerContext) {
	this.readerContext = readerContext;
	doRegisterBeanDeFinitions(doc.getDocumentElement());
}


    protected void doRegisterBeanDeFinitions(Element root) {
	BeanDeFinitionParserDelegate parent = this.delegate;
	this.delegate = createDelegate(getReaderContext(), root, parent);

	if (this.delegate.isDefaultNamespace(root)) { // 判断namespaceuri是否是http://www.springframework.org/schema/beans
		String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
		if (StringUtils.hasText(profileSpec)) { // profile属性存在
			String[] specifiedProfiles = StringUtils.tokenizetoStringArray(
					profileSpec, BeanDeFinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
			// We cannot use Profiles.of(...) since profile expressions are not supported
			// in XML config. See SPR-12458 for details.
			if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
				if (logger.isDebugEnabled()) {
					logger.debug("Skipped XML bean deFinition file due to specified profiles [" + profileSpec +
							"] not matching: " + getReaderContext().getResource());
				}
				return;
			}
		}
	}

	preProcessXml(root); // 钩子函数
	parseBeanDeFinitions(root, this.delegate);  // 解析xml文件里的标签
	postProcessXml(root); // 钩子函数

	this.delegate = parent;
}

十一、parseBeanDeFinitions

    protected void parseBeanDeFinitions(Element root, BeanDeFinitionParserDelegate delegate) {
	if (delegate.isDefaultNamespace(root)) {
		NodeList nl = root.getChildNodes();
		for (int i = 0; i < nl.getLength(); i++) {
			Node node = nl.item(i);
			if (node instanceof Element) {
				Element ele = (Element) node;
				if (delegate.isDefaultNamespace(ele)) {
					parseDefaultElement(ele, delegate); // 解析标签import,alias,bean,beans
				}
				else {
					delegate.parseCustomElement(ele); // 解析自定义标签
				}
			}
		}
	}
	else {
		delegate.parseCustomElement(root);
	}
}

以上是将xml的位置解析成Resource对象,在将Resource转成sax中解析xml的InputSource,读取成Document文档,解析Document文档。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。