Spring IOC source reading - bean definition parserdelegate

The function of bean definition parserdelegate is to generate bean instances based on the parsed file results. Next, we will learn the process of converting bean tags into bean instances.
Important: in this process, you can follow my comments on the source code tag step by step. You don't need to read all the source codes, so you should clear your mind first.

private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
        //From here, we start to transform the import tag
        if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
        //From here, we start the transformation of alias tags
        else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
        //From here, we start to transform bean tags
        else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
            processBeanDefinition(ele, delegate);
        //From here, we start the transformation of beans tags
        else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
            // recurse

The line commented in the above code is the method call of BeanDefinitionParserDelegate, that is, the beginning of bean tag transferring to bean instance. Click it to check

protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
        //Method name: analyze properties and convert to bean instance
        BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
        if (bdHolder != null) {
            bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
            try {
                // Register the final decorated instance.
                //Register bean instance
                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));

The first annotation is to parse and return an instance of BeanDefinitionHolder through the Element object, which is the predecessor of the bean and can also be understood as a shell,
The second annotation is to register the acquired bean call BeanDefinitionReaderUtils.registerBeanDefinition. We pay attention to the process from Element to bean, so enter the method of the first annotation

public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) {
        //The value of id is "id", which corresponds to the id attribute of the bean tag
        String id = ele.getAttribute(ID_ATTRIBUTE);
        //The value of name is "name", which corresponds to the name attribute of the bean tag
        String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);
        //Alias related
        List<String> aliases = new ArrayList<String>();
        if (StringUtils.hasLength(nameAttr)) {
            String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);

        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);
        //parseBeanDefinitionElement method overload, let's follow
        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)) {
                    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;

View the code for the overloaded parseBeanDefinitionElement

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);
        //How to create bean s
            AbstractBeanDefinition bd = createBeanDefinition(className, parent);

            parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
            bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));

            parseMetaElements(ele, bd);
            parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
            parseReplacedMethodSubElements(ele, bd.getMethodOverrides());

            parseConstructorArgElements(ele, bd);
            parsePropertyElements(ele, bd);
            parseQualifierElements(ele, bd);


            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 {

        return null;

In this method, we finally find the reflection get bean

public static AbstractBeanDefinition createBeanDefinition(
            String parentName, String className, ClassLoader classLoader) throws ClassNotFoundException {

        GenericBeanDefinition bd = new GenericBeanDefinition();
        if (className != null) {
            if (classLoader != null) {
//ClassUtils.forName reflection get bean instance
                bd.setBeanClass(ClassUtils.forName(className, classLoader));
            else {
        return bd;

The role of BeanDefinitionParserDelegate can be summarized as reflection, instantiation of bean, and registration into bean factory, where the level of call is also chaotic. Interested parties can take time to extract the application of inheritance hierarchy and design patterns.

Tags: xml Attribute shell Spring

Posted on Sun, 03 May 2020 18:00:04 -0400 by BigJohn