默认标签的解析过程(三)parseDefaultElement
阅读原文时间:2023年07月08日阅读:3
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {  
    if (delegate.nodeNameEquals(ele, IMPORT\_ELEMENT)) {  
        importBeanDefinitionResource(ele);  
    }  
    else if (delegate.nodeNameEquals(ele, ALIAS\_ELEMENT)) {  
        processAliasRegistration(ele);  
    }  
    else if (delegate.nodeNameEquals(ele, BEAN\_ELEMENT)) {  
        processBeanDefinition(ele, delegate);  
    }  
    else if (delegate.nodeNameEquals(ele, NESTED\_BEANS\_ELEMENT)) {  
        // recurse  
        doRegisterBeanDefinitions(ele);  
    }  
}

可以看出主要对(import,  alias, bean, beans进行解析)

IMPORT_ELEMENT

ALIAS_ELEMENT

BEAN_ELEMENT

NESTED_BEANS_ELEMENT
在上一篇文章DefaultListableBeanFactory只是保存一些bean解析出后的信息的注册基本信息到

beanDefinitionMap

/** Resolver to use for checking if a bean definition is an autowire candidate */
private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();

/\*\* Map from dependency type to corresponding autowired value \*/  
private final Map<Class<?>, Object> resolvableDependencies = new HashMap<Class<?>, Object>(16);

/\*\* Map of bean definition objects, keyed by bean。 name BeanDefinition主要是保存Bean 的名字,class 依赖等待信息\*/   

private final Map beanDefinitionMap = new ConcurrentHashMap(64);
/** Map of singleton bean names keyed by bean class */
private final Map, String[]> singletonBeanNamesByType = new ConcurrentHashMap, String[]>(64);
/** Map of non-singleton bean names keyed by bean class */
private final Map, String[]> nonSingletonBeanNamesByType = new ConcurrentHashMap, String[]>(64);
/** List of bean definition names, in registration order */
private final List beanDefinitionNames = new ArrayList();

 Bean解析:

/\*\*  
 \* Process the given bean element, parsing the bean definition  
 \* and registering it with the registry.  
 \*/  
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {  
    BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);//根据ele解析出所包含的所有信息  
    if (bdHolder != null) {  
        bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);  
        try {  
            // Register the final decorated instance.注册bdHold到beanDefinationMap  
            BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());  
        }  
        catch (BeanDefinitionStoreException ex) {  
            getReaderContext().error("Failed to register bean definition with name '" +  
                    bdHolder.getBeanName() + "'", ele, ex);  
        }  
        // Send registration event.  
        getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));  
    }  
}

/**
* Parses the supplied &lt;bean&gt; element. May return null
* if there were errors during parse. Errors are reported to the
* {@link org.springframework.beans.factory.parsing.ProblemReporter}.
*/
public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) {
return parseBeanDefinitionElement(ele, null);
}

/\*\*  
 \* Parses the supplied &lt;bean&gt; element. May return null  
 \* if there were errors during parse. Errors are reported to the  
 \* {@link org.springframework.beans.factory.parsing.ProblemReporter}.  
 \*/  
public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) {  
    String id = ele.getAttribute(ID\_ATTRIBUTE);  
    String nameAttr = ele.getAttribute(NAME\_ATTRIBUTE);

    List<String> aliases = new ArrayList<String>();  
    if (StringUtils.hasLength(nameAttr)) {  
        String\[\] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI\_VALUE\_ATTRIBUTE\_DELIMITERS);  
        aliases.addAll(Arrays.asList(nameArr));  
    }

    String beanName = id;  
    if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {  
        beanName = aliases.remove(0);  
        if (logger.isDebugEnabled()) {  
            logger.debug("No XML 'id' specified - using '" + beanName +  
                    "' as bean name and " + aliases + " as aliases");  
        }  
    }

    if (containingBean == null) {  
        checkNameUniqueness(beanName, aliases, ele);  
    }

    AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);  
    if (beanDefinition != null) {  
        if (!StringUtils.hasText(beanName)) {  
            try {  
                if (containingBean != null) {  
                    beanName = BeanDefinitionReaderUtils.generateBeanName(  
                            beanDefinition, this.readerContext.getRegistry(), true);  
                }  
                else {  
                    beanName = this.readerContext.generateBeanName(beanDefinition);  
                    // Register an alias for the plain bean class name, if still possible,  
                    // if the generator returned the class name plus a suffix.  
                    // This is expected for Spring 1.2/2.0 backwards compatibility.  
                    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);  
    }

    return null;  
}  

/**
* Parse the bean definition itself, without regard to name or aliases. May return
* null if problems occurred during the parsing of the bean definition.
*/
public AbstractBeanDefinition parseBeanDefinitionElement(
Element ele, String beanName, BeanDefinition containingBean) {

    this.parseState.push(new BeanEntry(beanName));

    String className = null;  
    if (ele.hasAttribute(CLASS\_ATTRIBUTE)) {  
        className = ele.getAttribute(CLASS\_ATTRIBUTE).trim();  
    }

    try {  
        String parent = null;  
        if (ele.hasAttribute(PARENT\_ATTRIBUTE)) {  
            parent = ele.getAttribute(PARENT\_ATTRIBUTE);  
        }  
        AbstractBeanDefinition bd = createBeanDefinition(className, parent); //返回GenericBeanDefinition 用于承载beanDefi

        parseBeanDefinitionAttributes(ele, beanName, containingBean, bd); //解析各种attr到bd  
        bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION\_ELEMENT));

        parseMetaElements(ele, bd);//<bean><meta..到bd  
        parseLookupOverrideSubElements(ele, bd.getMethodOverrides());  //<bean><lookup-method...到 bd  
        parseReplacedMethodSubElements(ele, bd.getMethodOverrides());//<bean><replaced-method 到bd 动态替换bean里面的方法

        parseConstructorArgElements(ele, bd);//<bean><constructor-arg... 到bd  
        parsePropertyElements(ele, bd);  //<bean><property....到bd  
        parseQualifierElements(ele, bd); //<bean><qualifier 到bd

        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();  
    }

    return null;  
}

alians 也与bean类似

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章