Spring IoC的细节--DefaultBeanDefinitionDocumentReader.createDelegate(...)

转载:
https://blog.csdn.net/sunchen2012/article/details/82415656

https://www.cnblogs.com/wzz1020/p/4753067.html


DefaultBeanDefinitionDocumentReader:

    protected void doRegisterBeanDefinitions(Element root) {
   
		//任何嵌套的<beans>元素都会在这个方法中引起递归。
		// 这种行为模拟了一堆委托,但实际上并不需要一个。
		// 为了正确地传播和保留<beans>default-*属性,请跟踪
		// 当前(父)委托,它可能为空。创建新的(子)委托
		// ,其中包含一个对父类的引用,以便进行回退
		// ,然后最终将this.delegate重置为其原始(父类)引用。
		// 这种行为模拟了一堆委托,但实际上并不需要一个。(有道翻译)

		//具体的解析过程由BeanDefinitionParserDelegate实现
		//BeanDefinitionParserDelegate中定义了Spring Bean定义XML文件的各种元素
		BeanDefinitionParserDelegate parent = this.delegate;
		this.delegate = createDelegate(getReaderContext(), root, parent); //代码1

		//假如配置了profile属性,则过滤环境配置是否匹配,不匹配则返回,该分支代码可忽略不看
		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)) {
   
					if (logger.isInfoEnabled()) {
   
						logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
								"] not matching: " + getReaderContext().getResource());
					}
					return;
				}
			}
		}

		//在解析Bean定义之前,进行自定义的解析,增强解析过程的可拓展性,默认0实现
		preProcessXml(root);

		//主线
		//使用Spring的Bean规则从Document的根元素开始进行Bean定义的解析
		parseBeanDefinitions(root, this.delegate);
		
		//在解析Bean定义之后,进行自定义的解析,增加解析过程的可拓展性,默认0实现
		postProcessXml(root);

		this.delegate = parent;
	}

代码1创建了一个BeanDefinitionParserDelegate对象,该对象是对XML中属性值解析的委派。

    protected BeanDefinitionParserDelegate createDelegate(
			XmlReaderContext readerContext, Element root, @Nullable BeanDefinitionParserDelegate parentDelegate) {
   

		BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
		delegate.initDefaults(root, parentDelegate);
		return delegate;
	}

我们看下BeanDefinitionParserDelegate类的常量值和成员变量:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值