• Spring源码解读:核心类DefaultListableBeanFactory的继承体系


    1 简介

    我们常用的ClassPathXmlApplicationContext是AbstractRefreshableApplicationContext的子类,而DefaultListableBeanFactory类型的beanFactory又是AbstractRefreshableApplicationContext的一个成员属性,也就是说ClassPathXmlApplicationContext运用了组合的方式扩展了DefaultListableBeanFactory的功能,使其自身成为了一个上下文容器。
    在这里插入图片描述
    另外XmlBeanFactory继承自DefaultListableBeanFactory,而DefaultListableBeanFactory是整个bean加载的核心部分,是 Spring 注册及加载 bean 的默认实现.
    在这里插入图片描述
    而对于 XmlBeanFactory与DefaultListableBeanFactory 不同的地方其实是在 XmlBeanFactory 中使用了自定义的XML 读取器XmlBeanDefinitionReader,实现了个性化的BeanDefinitionReader读取DefaultListableBeanFactory继承了 AbstractAutowireCapableBeanFactory 并实现了ConfigurableListableBeanFactory 以及BeanDefinitionRegistry接口。

    2 继承层次分析

    在这里插入图片描述

                                                                                                                                        DefaultListableBeanFactory的继承体系层次图

    各个类的主要功能和API:

    1)AliasRegistry

    定义对alias的简单增删改等操作

    //通用的别名管理接口
        public interface AliasRegistry {
    
    
             //Given a name, register an alias for it.
            void registerAlias(String name, String alias);
    
            /**
             * Remove the specified alias from this registry.
             */
            void removeAlias(String alias);
    
            /**
             * Determine whether this given name is defines as an alias
             * (as opposed to the name of an actually registered component).
             */
            boolean isAlias(String name);
    
            /**
             * Return the aliases for the given name, if defined.
             */
            String[] getAliases(String name);
        }

    2)SimpleAliasRegistry

    主要使用map作为alias的缓存,并对接口AliasRegistry进行实现.
    在这里插入图片描述

    3)SingletonBeanRegistry

    定义对单例的注册及获取

        //Interface that defines a registry for shared bean instances.
        public interface SingletonBeanRegistry {
    
            /**
             * Register the given existing object as singleton in the bean registry,
             * under the given bean name.
             */
            void registerSingleton(String beanName, Object singletonObject);
    
            /**
             * Return the (raw) singleton object registered under the given name.
             */
            @Nullable
            Object getSingleton(String beanName);
    
            /**
             * Check if this registry contains a singleton instance with the given name.
             */
            boolean containsSingleton(String beanName);
    
            /**
             * Return the names of singleton beans registered in this registry.
             */
            String[] getSingletonNames();
    
            /**
             * Return the number of singleton beans registered in this registry.
             */
            int getSingletonCount();
    
            /**
             * Return the singleton mutex used by this registry (for external collaborators).
             */
            Object getSingletonMutex();
        }

    4)BeanDefinitionRegistry

    定义对BeanDefinition的各种增删改操作

      /**
         * Interface for registries that hold bean definitions, for example RootBeanDefinition
         * and ChildBeanDefinition instances. Typically implemented by BeanFactories that
         * internally work with the AbstractBeanDefinition hierarchy.
         */
        public interface BeanDefinitionRegistry extends AliasRegistry {
    
            /**
             * Register a new bean definition with this registry.
             * Must support RootBeanDefinition and ChildBeanDefinition.
             */
            void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
                    throws BeanDefinitionStoreException;
    
            /**
             * Remove the BeanDefinition for the given name.
             */
            void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
    
            /**
             * Return the BeanDefinition for the given bean name.
             */
            BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
    
            /**
             * Check if this registry contains a bean definition with the given name.
             */
            boolean containsBeanDefinition(String beanName);
    
            /**
             * Return the names of all beans defined in this registry.
             */
            String[] getBeanDefinitionNames();
    
            /**
             * Return the number of beans defined in the registry.
             */
            int getBeanDefinitionCount();
    
            /**
             * Determine whether the given bean name is already in use within this registry,
             * i.e. whether there is a local bean or alias registered under this name.
             */
            boolean isBeanNameInUse(String beanName);
        }

    5)BeanFactory

    定义获取bean及bean的各种属性。
    在这里插入图片描述

    6)DefaultSingletonBeanRegistry

    对接口SingletonBeanRegistry各函数的实现
    在这里插入图片描述

    7)HierarchicalBeanFactory

    扩展了BeanFactory,新增了两个抽象方法,提供了对父工厂parentBeanFactory和内嵌Bean的支持

        /**
         * Sub-interface implemented by bean factories that can be part
         * of a hierarchy.
         */
        public interface HierarchicalBeanFactory extends BeanFactory {
    
            /**
             * Return the parent bean factory, or {@code null} if there is none.
             */
            BeanFactory getParentBeanFactory();
    
            /**
             * Return whether the local bean factory contains a bean of the given name,
             * ignoring beans defined in ancestor contexts.
             */
            boolean containsLocalBean(String name);
        }

    8)ListableBeanFactory

    提供了以类型、名字、注解等条件获取相关bean的各种配置信息的抽象方法。
    在这里插入图片描述

    9)FactoryBeanRegistrySupport

    在DefaultSingletonBeanRegistry基础上增加了对
    FactoryBean的特殊处理功能,没有新增的公用API,几乎全是protected级别的方法(主要是对FactoryBean的支持,FactoryBean不返回实例本身,而是返回此工厂方法getBean()的返回对象),以供子类调用或重写。
    在这里插入图片描述

    10)ConfigurableBeanFactory

    其公用API主要是以“setXXX"、“getXXX”、"registerXXX"开头的设定或获取配置的方法,它提供配置Bean的各种方法。在这里插入图片描述

                                            部分API图

    11)AbstractBeanFactory

    综合 FactoryBeanRegistrySupport与ConfigurableBeanFactory的功能,即综合了支持父工厂和配置bean的两种功能。
                      在这里插入图片描述

                                                                            部分API图

    12)AutowireCapableBeanFactory

    提供创建bean、自动注入、初始化以及应用bean的后处理器
    在这里插入图片描述

    13)ConfigurableListableBeanFactory

    BeanFactory配置清单,指定忽略类型及接口等
    在这里插入图片描述

    14)AbstractAutowireCapableBeanFactory

    综合AbstractBeanFactory并对接口AutowireCapableBeanFactory进行实现
                    
    在这里插入图片描述

                                              部分API图

    15)DefaultListableBeanFactory

    综合上面所有功能,主要是对Bean注册后的处理.

    参考:《Spring源码深度解析(第2版)》 

  • 相关阅读:
    WP7开发学习(3):在WP7中使用WCF+log4net 打印并输出日志文件(附源码)
    MongoDB开发学习(2)索引的基本操作
    C#批量添加水印
    获取某一时间戳的月份的开始时间戳和结束时间戳
    php判断一个字符串是否为日期格式
    正则表达式匹配括号引号内容
    PHP获取今日汽油价格接口
    ThinkPHP5 在Nginx环境下开启伪静态
    PHP格式化打印:JSON字符串|对象|数组
    php header ContentType出错
  • 原文地址:https://www.cnblogs.com/gocode/p/part01-inheritance-system-of-DefaultListableBeanFactory.html
Copyright © 2020-2023  润新知