spring 源码解读与设计详解:6 BeanDefinitionParserDelegate与资源解析

上篇文章中提到了spring资源加载的核心代码为;

DefaultBeanDefinitionDocumentReader类中的void registerBeanDefinitions(Document doc, XmlReaderContext readerContext)方法。

实质上XML文件的解析是由XMLBeanDefinitionReader对象交由BeanDefinitionParserDelegate委托来完成的,实质上这里面使用到了委托模式。

下面继续讲解registerBeanDefinitions方法,首先看代码:

/**
	 * This implementation parses bean definitions according to the "spring-beans" XSD
	 * (or DTD, historically).
	 * <p>Opens a DOM Document; then initializes the default settings
	 * specified at the {@code <beans/>} level; then parses the contained bean definitions.
	 */
	@Override
	public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
		this.readerContext = readerContext;
		logger.debug("Loading bean definitions");
		Element root = doc.getDocumentElement();
		doRegisterBeanDefinitions(root);
	}

解析过程为doRegisterBeanDefinitions(root)

/**
	 * Register each bean definition within the given root {@code <beans/>} element.
	 */
	protected void doRegisterBeanDefinitions(Element root) {
		// Any nested <beans> elements will cause recursion in this method. In
		// order to propagate and preserve <beans> default-* attributes correctly,
		// keep track of the current (parent) delegate, which may be null. Create
		// the new (child) delegate with a reference to the parent for fallback purposes,
		// then ultimately reset this.delegate back to its original (parent) reference.
		// this behavior emulates a stack of delegates without actually necessitating one.
		BeanDefinitionParserDelegate parent = this.delegate;
		this.delegate = createDelegate(getReaderContext(), root, parent);

		if (this.delegate.isDefaultNamespace(root)) {
			String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
			if (StringUtils.hasText(profileSpec)) {
				String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
						profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
				if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
					return;
				}
			}
		}

		preProcessXml(root);
		parseBeanDefinitions(root, this.delegate);
		postProcessXml(root);

		this.delegate = parent;
	}

看最后几行代码:

preProcessXml(root);
		parseBeanDefinitions(root, this.delegate);
		postProcessXml(root);

		this.delegate = parent;

这里的preProcessXml和postProcessXml都是空实现,这个doRegisterBeanDefinitions方法使用了经典的模板方法设计模式,子类可以重写
preProcessXml和
postProcessXml,实现对XML配置文件的自定义扩展。类似于Junit的setUp、testXXX、tearDown方法

而Parent这里,则是实现了beans的嵌套。


    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/sundenskyqq/article/details/45368481
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞