Spring框架学习[IoC容器解析Bean](二)

2014-11-24 03:05:58 · 作者: · 浏览: 1
isDefaultNamespace(ele)) { //使用Spring的Bean规则解析元素节点
  • parseDefaultElement(ele, delegate); }
  • else { //没有使用Spring默认的XML命名空间,则使用用户自定义的解//析规则解析元素节点
  • delegate.parseCustomElement(ele); }
  • } }
  • } else {
  • //Document的根节点没有使用Spring默认的命名空间,则使用用户自定义的 //解析规则解析Document根节点
  • delegate.parseCustomElement(root); }
  • } //使用Spring的Bean规则解析Document元素节点
  • private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) { //如果元素节点是 导入元素,进行导入解析
  • if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) { importBeanDefinitionResource(ele);
  • } //如果元素节点是 别名元素,进行别名解析
  • else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) { processAliasRegistration(ele);
  • } //元素节点既不是导入元素,也不是别名元素,即普通的 元素,
  • //按照Spring的Bean规则解析元素 else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
  • processBeanDefinition(ele, delegate); }
  • } //解析 导入元素,从给定的导入路径加载Bean定义资源到Spring IoC容器中
  • protected void importBeanDefinitionResource(Element ele) { //获取给定的导入元素的location属性
  • String location = ele.getAttribute(RESOURCE_ATTRIBUTE); //如果导入元素的location属性值为空,则没有导入任何资源,直接返回
  • if (!StringUtils.hasText(location)) { getReaderContext().error(Resource location must not be empty, ele);
  • return; }
  • //使用系统变量值解析location属性值 location = SystemPropertyUtils.resolvePlaceholders(location);
  • Set actualResources = new LinkedHashSet (4); //标识给定的导入元素的location是否是绝对路径
  • boolean absoluteLocation = false; try {
  • absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute(); }
  • catch (URISyntaxException ex) { //给定的导入元素的location不是绝对路径
  • } //给定的导入元素的location是绝对路径
  • if (absoluteLocation) { try {
  • //使用资源读入器加载给定路径的Bean定义资源 int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);
  • if (logger.isDebugEnabled()) { logger.debug(Imported + importCount + bean definitions from URL location [ + location + ]);
  • } }
  • catch (BeanDefinitionStoreException ex) { getReaderContext().error(
  • Failed to import bean definitions from URL location [ + location + ], ele, ex); }
  • } else {
  • //给定的导入元素的location是相对路径 try {
  • int importCount; //将给定导入元素的location封装为相对路径资源
  • Resource relativeResource = getReaderContext().getResource().createRelative(location); //封装的相对路径资源存在
  • if (relativeResource.exists()) { //使用资源读入器加载Bean定义资源
  • importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource); actualResources.add(relativeResource);
  • } //封装的相对路径资源不存在
  • else { //获取Spring IoC容器资源读入器的基本路径
  • String baseLocation = getReaderContext().getResource().getURL().toString(); //根据Spring IoC容器资源读入器的基本路径加载给定导入
  • //路径的资源 importCount = getReaderContext().getReader().loadBeanDefinitions(
  • StringUtils.applyRelativePath(baseLocation, location), actualResources); }
  • if (logger.isDebugEnabled()) { logger.debug(Imported + importCount + bean definitions from relative location [ + location + ]);
  • } }
  • catch (IOException ex) { getReaderContext().error(Failed to resolve current resource location, ele, ex);
  • } catch (BeanDefinitionStoreException ex) {
  • getReaderContext().error(Failed to import bean definitions from relative location [ + location + ], ele, ex);
  • } }
  • Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]); //在解析完 元素之后,发送容器导入其他资源处理完成事件
  • getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele)); }