基于XML文件读取bean定义:XmlBeanDefinitionReader

[b]基于XML文件读取bean定义[/b]
除了基于Properties文件读取bean的定义,这里介绍另一种读取bean的定义方式。这中方式更为常见。
[b]XmlBeanDefinitionReader[/b]
[b]主要方法介绍:[/b]
[b]loadBeanDefinitions();//加载bean的定义,同样是通过用被指定的编码方式来读取流xml资源文件。同样返回定义的bean的数量[/b]
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isInfoEnabled()) {
logger.info("Loading XML bean definitions from " + encodedResource.getResource());
}

try {
InputStream inputStream = encodedResource.getResource().getInputStream();
try {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {//如果指定了编码方式,就用指定的编码为流的编码 inputSource.setEncoding(encodedResource.getEncoding());
}//这类调用该方式真正的做到了加载bean定义
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {//关闭流
inputStream.close();
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
}


[b]doLoadBeanDefinitions();该方法真正完成加载bean定义的工作[/b]
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {//取得xml文件的文档验证方式,比如dtd,xsd。
int validationMode = getValidationModeForResource(resource);//加载器加载xml文件。
Document doc = this.documentLoader.loadDocument(
inputSource, this.entityResolver, this.errorHandler, validationMode, this.namespaceAware);//调用下面介绍的方法注册bean到工厂
return registerBeanDefinitions(doc, resource);
}
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (ParserConfigurationException ex) {
throw new BeanDefinitionStoreException(
"Parser configuration exception parsing XML from " + resource, ex);
}
catch (SAXParseException ex) {
throw new BeanDefinitionStoreException(
"Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
}
catch (SAXException ex) {
throw new BeanDefinitionStoreException("XML document from " + resource + " is invalid", ex);
}
catch (IOException ex) {
throw new BeanDefinitionStoreException("IOException parsing XML document from " + resource, ex);
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException("Unexpected exception parsing XML document from " + resource, ex);
}
}

[b]registerBeanDefinitions();这里完成注册bean到工厂的工作。[/b]
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
if (this.parserClass != null) {//拿到解析器负责从xml解析定义的bean,并负责将它们注册到工厂
XmlBeanDefinitionParser parser =
(XmlBeanDefinitionParser) BeanUtils.instantiateClass(this.parserClass);
return parser.registerBeanDefinitions(this, doc, resource);
}//若解析器为null,就创建一个documentReader ,把解析bean定义并注册bean到工厂的工作交给它完成
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
int countBefore = getBeanFactory().getBeanDefinitionCount();
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getBeanFactory().getBeanDefinitionCount() - countBefore;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值