转载:
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