2.8 解析以及注册BeanDefintitions
当文件转换成Document后,接下来的是提取以及注册bean就是我们的重头戏。当程序以及拥有Document实例对象的时候,就会引入下面的方法。
//解析以及注册BeanDefinitions
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
//实例化一个documentReader 意思是将解析处理Bean的事情交给了documentReader,
//documentReader这是个接口,由他的子类DefaultBeanDefinitionDocumentReader来真正的完成
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
//记录统计前BeanDefinition的加载个数
int countBefore = getRegistry().getBeanDefinitionCount();
//加载及注册Bean
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
//记录本次加载的个数
return getRegistry().getBeanDefinitionCount() - countBefore;
}
doc是之前loadDocument加载转换来的,看一下registerBeanDefinitions()方法
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
this.readerContext = readerContext;
doRegisterBeanDefinitions(doc.getDocumentElement());
}
@SuppressWarnings("deprecation") // for Environment.acceptsProfiles(String...)
protected void doRegisterBeanDefinitions(Element root) {
//专门处理解析
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);
if (this.delegate.isDefaultNamespace(root)) {
//处理profile属性
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
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);
//解析后处理 留给子类实现
postProcessXml(root);
this.delegate = parent;
}
上述代码主要是来处理了一下profile属性,然后开始解析
2.8.1 profile属性的使用
public static final String PROFILE_ATTRIBUTE = "profile";
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<beans profile="dev">
</beans>
<beans profile="production>
</beans>
</beans>
集成到web中的是
<context-param>
<param-name>Spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
有了这个特性的话,我们可以同时在配置文件中部署两套配置来使用生产环境和开发环境,最常用的是切换不同的数据库
2.8.2 解析并注册BeanDefinition
处理完profile之后就是对XML进行读取了,跟踪代码进入到
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)) {
//对bean进行处理
parseDefaultElement(ele, delegate);
}
else {
//对非bean进行处理
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
Spring的XML配置中有两大类Bean声明,一类是默认的,一类是自定义的
子节点是默认的命名空间的话采用的是parseDefaultElement(ele, delegate);方法进行解析
自定义的命名空间采用的是delegate.parseCustomElement(ele);进行解析
...接下来 我们看看对这两种Bean声明的解析