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

2014-11-24 03:05:58 · 作者: · 浏览: 3
的id、name或者别名是否重复 checkNameUniqueness(beanName, aliases, ele);
  • } //详细对 元素中配置的Bean定义进行解析的地方
  • AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean); if (beanDefinition != null) {
  • if (!StringUtils.hasText(beanName)) { try {
  • if (containingBean != null) { //如果 元素中没有配置id、别名或者name,且没有包含子// 元素,为解析的Bean生成一个唯一beanName并注册
  • beanName = BeanDefinitionReaderUtils.generateBeanName( beanDefinition, this.readerContext.getRegistry(), true);
  • } else {
  • //如果 元素中没有配置id、别名或者name,且包含了子// 元素,为解析的Bean使用别名向IoC容器注册 beanName = this.readerContext.generateBeanName(beanDefinition);
  • //为解析的Bean使用别名注册时,为了向后兼容 //Spring1.2/2.0,给别名添加类名后缀 String beanClassName = beanDefinition.getBeanClassName();
  • if (beanClassName != null && beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&
  • !this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) { aliases.add(beanClassName);
  • } }
  • if (logger.isDebugEnabled()) { logger.debug(Neither XML 'id' nor 'name' specified - +
  • using generated bean name [ + beanName + ]); }
  • } catch (Exception ex) {
  • error(ex.getMessage(), ele); return null;
  • } }
  • String[] aliasesArray = StringUtils.toStringArray(aliases); return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);
  • } //当解析出错时,返回null
  • return null; }
  • //详细对 元素中配置的Bean定义其他属性进行解析,由于上面的方法中已经对//Bean的id、name和别名等属性进行了处理,该方法中主要处理除这三个以外的其他属性数据 public AbstractBeanDefinition parseBeanDefinitionElement(
  • Element ele, String beanName, BeanDefinition containingBean) { //记录解析的
  • this.parseState.push(new BeanEntry(beanName)); //这里只读取 元素中配置的class名字,然后载入到BeanDefinition中去
  • //只是记录配置的class名字,不做实例化,对象的实例化在依赖注入时完成 String className = null;
  • if (ele.hasAttribute(CLASS_ATTRIBUTE)) { className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
  • } try {
  • String parent = null; //如果 元素中配置了parent属性,则获取parent属性的值
  • if (ele.hasAttribute(PARENT_ATTRIBUTE)) { parent = ele.getAttribute(PARENT_ATTRIBUTE);
  • } //根据 元素配置的class名称和parent属性值创建BeanDefinition
  • //为载入Bean定义信息做准备 AbstractBeanDefinition bd = createBeanDefinition(className, parent);
  • //对当前的 元素中配置的一些属性进行解析和设置,如配置的单态(singleton)属性等 parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
  • //为 元素解析的Bean设置description信息 bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT)); //对 元素的meta(元信息)属性解析
  • parseMetaElements(ele, bd); //对 元素的lookup-method属性解析
  • parseLookupOverrideSubElements(ele, bd.getMethodOverrides()); //对 元素的replaced-method属性解析
  • parseReplacedMethodSubElements(ele, bd.getMethodOverrides()); //解析 元素的构造方法设置
  • parseConstructorArgElements(ele, bd); //解析 元素的 设置
  • parsePropertyElements(ele, bd); //解析 元素的qualifier属性
  • parseQualifierElements(ele, bd); //为当前解析的Bean设置所需的资源和依赖对象
  • bd.setResource(this.readerContext.getResource()); bd.setSource(extractSource(ele));
  • return bd; }
  • catch (ClassNotFoundException ex) { error(Bean class [ + className + ] not found, ele, ex);
  • } catch (NoClassDefFoundError err) {
  • error(Class that bean class [ + className + ] depends on not found, ele, err); }
  • catch (Throwable ex) { error(Unexpected failure during bean definition parsing, ele, ex);
  • } finally {
  • this.parseState.pop(); }
  • //解析 元素出错时,返回null return null;
  • }

    只要使用过Spring,对Spring配置文件比较熟悉的人