上一节:http://blog.csdn.net/disiwei1012/article/details/75452356
继续上次的分析:
XmlBeanDefinitionReader:
@SuppressWarnings("deprecation")
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
//设置上下文环境
documentReader.setEnvironment(getEnvironment());
//记录加载的beanDefinition个数
int countBefore = getRegistry().getBeanDefinitionCount();
//注册bean
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
//记录本次加载的beanDefinition个数
return getRegistry().getBeanDefinitionCount() - countBefore;
}
创建DocumentReader (createBeanDefinitionDocumentReader),允许用户自定义DocumentReader,但必须要实现BeanDefinitionDocumentReader接口。
protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader() {
return BeanDefinitionDocumentReader.class.cast(BeanUtils.instantiateClass(this.documentReaderClass));
}
注:
1.beanDefinition 相当于XML中的bean标签
2.BeanDefinitionDocumentReader.class.cast(Obj A) :强制将A转化成BeanDefinitionDocumentReader类型
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
this.readerContext = readerContext;
logger.debug("Loading bean definitions");
Element root = doc.getDocumentElement();
//真正开始解析的地方
doRegisterBeanDefinitions(root);
}
/**
* Register each bean definition within the given root {@code <beans/>} element.
*/
protected void doRegisterBeanDefinitions(Element root) {
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
if (!getEnvironment().acceptsProfiles(specifiedProfiles)) {
return;
}
}
// 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(this.readerContext, root, parent);
preProcessXml(root);
//开始解析
parseBeanDefinitions(root, this.delegate);
postProcessXml(root);
this.delegate = parent;
}
注册根节点root()中的每一个beanDefinition。
1.首先获取根节点beans的profile属性值。值与值之间可以使用逗号或者分号分隔。
2.然后判断profile是否都符合环境变量中所定义的,不符合则不会去解析。
假如:
acceptsProfiles(“dev”,”pro”) :如果dev和pro均被激活则返回true,否则返回false
acceptsProfiles(“dev”,”!pro”) :如果dev被激活,pro未被激活返回true,否则返回false
激活的方式有:
①JVM参数
set JAVA_OPTS="-Dspring.profiles.active=pro"
②web.xml
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
等等。
3.创建解析代理对象delegate,将解析工作委托给delegate
4.preProcessXml、postProcessXml方法体均为空,模板方法模式。
parseBeanDefinitions方法是真正解析开始的地方!
模板方法模式请参考:http://blog.csdn.net/disiwei1012/article/details/53467251
代理模式请参考:http://blog.csdn.net/disiwei1012/article/details/53440501
对于beans的profile具体用法参考我的这篇博客:http://blog.csdn.net/disiwei1012/article/details/75716369