前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >深入理解Spring源码(一)-IOC容器的定位,载入,注册

深入理解Spring源码(一)-IOC容器的定位,载入,注册

作者头像
Meet相识
发布2018-09-12 16:48:03
1.4K0
发布2018-09-12 16:48:03
举报
文章被收录于专栏:技术专栏技术专栏

前言:Spring源码继承,嵌套层次非常多,读起来非常容易晕,小伙伴们在看文章的时候一定要跟着文章的思路自己去源码里点一点,看一看,并且多看几次。就会越来越清晰。下面开始正题

1.Spring的几个核心类

1.1.BeanFactory

1

Spring的IOC容器工厂中,BeanFactory是最顶层的一个接口类,他定义了IOC容器的基本规范,BeanFactory有三个子类:ListableBeanFactory(可列表化的),HierarchialBeanFactory(可分层的的),AutowireCapableBeanFactory(可自动注入的),但是从图中可以发现,他们最终的实现类是DefaultListableBeanFactory,他实现了所有的接口,那为什么要定义那么多层次的接口呢?查阅源码和说明可以发现,每一个接口都有他适用的场合,他主要是为了区分Spring内部在操作过程中对象的传递和转化过程中,对对象的数据访问所做的限制。例如ListableBeanFactory接口表示这些bean是可列表的;而HierarchialBeanFactory表示的是这些bean是有继承关系的,也就是说每个bean都可能有父bean;AutowireCapableBeanFactory定义bean的自动装配规则。这四个接口共同定义了bean的集合、bean之间的关系、以及bean的行为。

代码语言:javascript
复制
public interface BeanFactory {

     /**
      * 对factorybean的转义定义,因为如果使用bean的名字检索factorybean得到的对象是工厂生成的对象,
      * 如果需要得到工程本身,需要转义
      */
    String FACTORY_BEAN_PREFIX = "&";
     /**
      * 根据bean的名字,获取在IOC容器中得到bean实例
      */
    Object getBean(String name) throws BeansException;
     /**
      *  根据bean的名字和class类型来得到bean实例,增加了类型安全验证机制
      * 如果需要得到工程本身,需要转义
      */
     <T> T getBean(String name, Class<T> requiredType) throws BeansException;


     /**
      * 根据bean的名字和类构造函数参数类得到bean实例,这里针对的是prototype类型
      */
    Object getBean(String name, Object... args) throws BeansException;
     /**
      * 根据bean的类型和类构造函数参数类得到bean实例,这里针对的是prototype类型
      */
    <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

      /**
        * 提供对bean的检索,看看在IOC容器中是否有这个名字的bean
        */
    boolean containsBean(String name);

      /**
        * 根据bean的名字得到bean实例,并判断这个bean是否是单例模式
        */ 
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

      /**
        * 根据bean的名字得到bean实例,并判断这个bean是否是原型模式
        */ 
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

       /**
        * 判断某一个bean是否是指定类型的
        */ 
    boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;

       /**
        * 得到bean实例的class类型
        */ 
    Class<?> getType(String name) throws NoSuchBeanDefinitionException;

       /**
        * 得到bean的别名,如果使用别名检索,那么其原名也会被检索出来
        */ 
    String[] getAliases(String name);

}

在BeanFactory里只对IOC容器的基本行为做了定义,根本不关系bean是怎样定义如何加载的。正如我们只关心工厂里得到的产品对象,至于工厂是怎么生产产品的,这个接口并不关心。

而要知道工厂是如何产生对象的,我们需要看IOC容器的具体实现。Spring提供了很多容器的实现。比如XmlBeanFactory,ClasspathXmlApplicationContext等,其中XmlBeanFactory就是针对最基本的IOC容器的实现,这个IOC容器可以读取XML文件定义的BeanDefinition(XML文件对bean的描述),如果是XmlBeanFactory是容器中的屌丝,ApplicationContext应该算容器中的高富帅。

ApplicationContext是Spring提供的一个高级的IOC容器,它除了能提供IOC容器的基本功能外,还为用户提供了以下的附加服务。

从ApplicationContext接口的实现,我们可以看出其特点: 1. 支持信息源,可以实现国际化。(实现MessageResource接口) 2. 访问资源,(实现ResourcePatternResolver接口) 3. 支持应用事件。(实现ApplicationEventPublisher)

1.2 BeanDefinition

SpringIOC容器管理了我们定义的各种Bean对象以及相互依赖的关系,Bean对象在Spring容器中是以BeanDefinition描述的,可以说BeanDefinition就是Spring重点的pojo对象,其继承体系如下

1.2-1

1.3 BeanDefinitionReader

Bean的解析过程非常复杂,功能被分的很细,因为里面被扩展的东西很多,必须保证有足够的灵活性

1.3-1


2.IOC容器的初始化

IOC容器的初始化包括BeanDefinition的Resource定位、载入、注册的过程。我们以ApplicationContext为例讲解,ApplicationContext系列容器也许是我们最熟悉的,因为Web项目使用的XmlWebApplicaitonContext就属于这个体系,还有ClasspathXMLApplicationContext等,其继承体系如下图所示

2-1

ApplicationContext允许上下文嵌套,通过保持父上下文可以维持一个上下文体系。对于bean的查找可以在这个上下文体系中发生,首先检查当前上下文,其次是父上下文,逐级向上,这样为不同的Spring应用提供了一个共享的bean定义环境

首先我们演示一下XmlBeanFactory(屌丝IOC)的整个流程,通过XmlBeanFactory的源码,我们可以发现,这个类实际上已经废弃了,而他的主要流程实际上就是维护了一个XmlBeanDefinitionReader,并将resource资源设置进去,来完成初始化,其中XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); ,其中this传的是factory对象

代码语言:javascript
复制
@Deprecated
@SuppressWarnings({"serial", "all"})
public class XmlBeanFactory extends DefaultListableBeanFactory {

    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);


    /**
     * Create a new XmlBeanFactory with the given resource,
     * which must be parsable using DOM.
     * @param resource XML resource to load bean definitions from
     * @throws BeansException in case of loading or parsing errors
     */
    public XmlBeanFactory(Resource resource) throws BeansException {
        this(resource, null);
    }

    /**
     * Create a new XmlBeanFactory with the given input stream,
     * which must be parsable using DOM.
     * @param resource XML resource to load bean definitions from
     * @param parentBeanFactory parent bean factory
     * @throws BeansException in case of loading or parsing errors
     */
    public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
        super(parentBeanFactory);
        this.reader.loadBeanDefinitions(resource);
    }

}

我们来编程试的实现一下整个流程

2-2

接下来,开始正式分析我们的ApplicationContext初始化流程,我们以FileXmlApplicationContext为入口分析,一共分为20小步

  • 1-7 为资源定位
  • 8-15 为载入
  • 16 为注册

2.1 高富帅IOC解剖

FileXmlApplicationContext提供了多个构造函数,不过最终都是调用的是这个构造函数

代码语言:javascript
复制
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
            throws BeansException {
                // 为了动态的确定用哪个加载器去加载我们的文件
        super(parent);
                // 告诉读取配置文件放在哪里:定位,为了加载配置
        setConfigLocations(configLocations);
                // 刷新
        if (refresh) {
            refresh();
        }
    }

2.2设置资源加载器和资源定位

通过分析FileSystemXmlApplicationContext的源码可以知道,在创建FileSystemXmlApplicationContext容器时,构造函数主要做两项重要工作:

首先调用父类构造方法super(parent)为容器设置好Bean资源加载器。

然后,再调用父类AbstractRefreshableConfigApplicationContext的setConfigLocations方法设置bean定义资源文件的定位路径

通过追踪FileSystemXmlApplicationContext的继承体系,发现其父类的父类AbstractApplicationContext中初始化IOC容器所做的主要源码如下:

代码语言:javascript
复制
public abstract class AbstractApplicationContext extends DefaultResourceLoader
        implements ConfigurableApplicationContext, DisposableBean {
    static {
        // Eagerly load the ContextClosedEvent class to avoid weird classloader issues
        // on application shutdown in WebLogic 8.1. (Reported by Dustin Woods.)
                // 为了避免应用程序在WebLogic 8.1关闭时出现类异常加载问题,加载IOC容器关闭时间(ContextClosedEvent类)
        ContextClosedEvent.class.getName();
    }

    /**
     * Create a new AbstractApplicationContext with no parent.
     */
    public AbstractApplicationContext() {
                // 解析我们的资源文件,动态匹配过程
        this.resourcePatternResolver = getResourcePatternResolver();
    }

    /**
     * Create a new AbstractApplicationContext with the given parent context.
         *  FileSystemXmlApplicationContext调用父类的构造方法就是调用的改方法
     * @param parent the parent context
     */
    public AbstractApplicationContext(ApplicationContext parent) {
        this();
        setParent(parent);
    }

        /**
     *  获取一个Spring Resource的加载器用于读入Spring Bean定义资源文件
         *  AbstractApplicationContext类继承了DefaultResourceLoader,所以具有资源加载的能力,这里的this代表的就是DefaultResourceLoader对象
     */
        protected ResourcePatternResolver getResourcePatternResolver() {
        return new PathMatchingResourcePatternResolver(this);
    }
}

AbstractApplicationContext 的构造方法中调用PathMatchingResourcePatternResolver构造方法创建Spring资源加载器。

代码语言:javascript
复制
public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
        Assert.notNull(resourceLoader, "ResourceLoader must not be null");
                // 设置Spring的资源加载器
        this.resourceLoader = resourceLoader;
    }
代码语言:javascript
复制
    String CONFIG_LOCATION_DELIMITERS = ",; \t\n";
/**
     * Set the config locations for this application context in init-param style,
     * i.e. with distinct locations separated by commas, semicolons or whitespace.
     * <p>If not set, the implementation may use a default as appropriate.
     */
    public void setConfigLocation(String location) {
                // 即多个文件之间用, ; \t \n 分割,解析成数组形式
        setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));
    }

    /**
     * Set the config locations for this application context.
     * <p>If not set, the implementation may use a default as appropriate.
         * 解析bean定义资源文件的路径,处理多个资源文件字符串数组
     */
    public void setConfigLocations(String... locations) {
        if (locations != null) {
            Assert.noNullElements(locations, "Config locations must not be null");
            this.configLocations = new String[locations.length];
            for (int i = 0; i < locations.length; i++) {
                                 // resolvePath为同一个类中讲字符串解析为路径的方法
                this.configLocations[i] = resolvePath(locations[i]).trim();
            }
        }
        else {
            this.configLocations = null;
        }
    }

通过这两个方法的源码我们可以看出,我们即可以使用一个字符串来配置多个Spring Bean定义资源文件,也可以使用字符串数组。

至此,Spring IOC容器在初始化时将配置的Bean定义资源文件定位为Spring封装的Resource

2.3 AbstractApplicationContext的refresh函数载入Bean定义过程

Spring IOC容器对Bean定义资源的载入是从refresh方法开始的,refresh()是一个模板方法,refresh()方法的作用是:在创建IOC容器之前,如果已经有容器存在,则需要把已有的容器销毁并关闭,以保证在refresh()之后使用的是新建立起来的IOC容器,refresh()的作用类似于对IOC容器的重启,在新建立好的容器中对容器进行初始化,对Bean定义资源进行载入FileXmlApplicationContext通过调用起父类AbstractApplicationContext的refresh函数启动整个IOC容器对Bean定义的载入过程

代码语言:javascript
复制
// 容器初始化的过程,读入Bean定义资源,并解析注册
@Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            // 调用容器准备刷新的方法,获取容器的当前时间,同时给容器设置同步标示
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            //告诉子类启动refreshBeanFactory方法,Bean定义资源文件的载入从子类的refreshBeanFactory方法开始
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
// 为BeanFactory配置特性,例如类加载器、事件处理器等
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                // 为容器的某些子类指定特殊的Beanpost事件处理器
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                // 调用所有注册的BeanFactoryPostProcessor的Bean
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                // BeanPostProcessor是Bean的后置处理器,用于监听容器触发的事件
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                // 初始化信息源,和国际化相关
                initMessageSource();

                // Initialize event multicaster for this context.
                // 初始化容器时间传播器
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                // 调用子类的某些特殊Bean初始化方法
                onRefresh();

                // Check for listener beans and register them.
                // 为事件传播器注册事件监听器
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                // 初始化所有剩余的单例bean
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                // 初始化容器的生命周期事件处理器,并发布容器的生命周期事件
                finishRefresh();
            }

            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }

                // Destroy already created singletons to avoid dangling resources.
                // 销毁已经创建的单态bean
                destroyBeans();

                // Reset 'active' flag.
                // 取消refresh操作,重置容器的同步标示
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }

            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

refresh方法主要为IOC容器Bean的声明周期管理提供条件,Spring IOC容器载入Bean定义资源文件从其子类容器的refreshBeanFactory开始,所以整个refresh()中ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); 这句以后的代码都是注册容器的信息源和生命周期事件,载入过程就是从这句代码启动。

2.4 具体的refreshBeanFactory方法执行过程

AbstractApplicationContext的obtainFreshBeanFactory()方法调用子类的refreshBeanFactory方法。启动容器载入Bean定义资源文件的过程,代码如下:

代码语言:javascript
复制
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
// 这里使用了模板方法模式,父类定义了抽象的refreshBeanFactory方法,具体实现由子类完成
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {
            logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
        }
        return beanFactory;
    }

AbstractApplicationContext类只抽象定义了refreshBeanFactory()方法,容器真正调用AbstractRefreshableApplicationContext实现的方法,其源码如下

代码语言:javascript
复制
@Override
    protected final void refreshBeanFactory() throws BeansException {
        if (hasBeanFactory()) { //如果已经存在容器,销毁容器中的bean,关闭容器
            destroyBeans();
            closeBeanFactory();
        }
        try {
            // 创建IOC容器
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            beanFactory.setSerializationId(getId());
            // 对IOC容器进行定制化,如设置启动参数,开启注解的自动装配等
            customizeBeanFactory(beanFactory);
           // 调用载入的Bean定义的方法,这里又使用了一个模板模式
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }

在这个方法中,先判断BeanFactory是否存在,如果存在则销毁,接着创建DefaultListableBeanFactory,并调用loadBeanDefinitions(beanFactory)装在bean定义。

2.5 AbstractRefreshableApplicationContext子类的loadBeanDefinitions方法

AbstractRefreshableApplicationContext只定义了抽象的loadBeanDefinitions方法,容器真正调用的是子类AbstractXmlApplicationContext对该方法的实现,AbstractXmlApplicationContext的主要源码如下:

代码语言:javascript
复制
    @Override
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
        // Create a new XmlBeanDefinitionReader for the given BeanFactory.
        // 创建XmlBeanDefinitionReader,即创建Bean读取器,并通过回调设置到容器中去,容器使用该读取器读取
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

        // Configure the bean definition reader with this context's
        // resource loading environment.
        beanDefinitionReader.setEnvironment(this.getEnvironment());

        // 为Bean读取器位置Spring资源加载器,AbstractRefreshableApplicationContext的
        // 祖先父类AbstractRefreshableApplicationContext继承DefaultResourceReader,因此,容器本身也是一个资源
        beanDefinitionReader.setResourceLoader(this);
        // 为Bean读取器设置SAX xml解析器
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

        // Allow a subclass to provide custom initialization of the reader,
        // then proceed with actually loading the bean definitions.
        // 当Bean读取器读取Bean定义的Xml资源文件时,启用Xml的校验机制
        initBeanDefinitionReader(beanDefinitionReader);
        // Bean 读取器真正实现加载的方法
        loadBeanDefinitions(beanDefinitionReader);
    }
代码语言:javascript
复制
// Bean 读取器加载Bean定义资源
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
        // 获取bean定义资源的方位
        // 这里使用模板模式,调用子类获取Bean定为资源的方法
        // 该方法在ClassPathXmlApplicationContext中进行实现,对于我们的
        // 示例FileXmlApplicationContext没有使用该方法
        Resource[] configResources = getConfigResources();
        if (configResources != null) {
         // XML Bean读取器调用其父类的AbstractBeanDefinitionReader读取定位的Bean定义资源
            reader.loadBeanDefinitions(configResources);
        }
         // 如果子类中获取的Bean定位资源为空,则获取FileXmlApplicationContext构造方法中setConfigLocations这是的string资源路径
        String[] configLocations = getConfigLocations();
        if (configLocations != null) {
         // Xml Bean读取器调用其父类AbstractBeanDefinitionReader读取定位的Bean定义资源
            reader.loadBeanDefinitions(configLocations);
        }
    }

在其抽象父类 AbstractBeanDefinitionReader中定义了载入过程

代码语言:javascript
复制
    /**
     * 重载父类方法,调用下面的loadBeanDefinitions(String location, Set<Resource> actualResources)方法
     */
    @Override
    public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
        return loadBeanDefinitions(location, null);
    }

    /**
     * Load bean definitions from the specified resource location.
     * <p>The location can also be a location pattern, provided that the
     * ResourceLoader of this bean definition reader is a ResourcePatternResolver.
     * @param location the resource location, to be loaded with the ResourceLoader
     * (or ResourcePatternResolver) of this bean definition reader
     * @param actualResources a Set to be filled with the actual Resource objects
     * that have been resolved during the loading process. May be {@code null}
     * to indicate that the caller is not interested in those Resource objects.
     * @return the number of bean definitions found
     * @throws BeanDefinitionStoreException in case of loading or parsing errors
     * @see #getResourceLoader()
     * @see #loadBeanDefinitions(org.springframework.core.io.Resource)
     * @see #loadBeanDefinitions(org.springframework.core.io.Resource[])
     */
    public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
        // 在IOC容器初始化过程中设置的资源加载器,见2.2步
        // 加载多个指定位置的Bean定义资源文件
        ResourceLoader resourceLoader = getResourceLoader();
        if (resourceLoader == null) {
            throw new BeanDefinitionStoreException(
                    "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
        }

        if (resourceLoader instanceof ResourcePatternResolver) {
            // Resource pattern matching available.
            try {
                Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
                // 委派使用其子类的XmlBeanDefinitionReader的方法,实现加载功能
                int loadCount = loadBeanDefinitions(resources);
                if (actualResources != null) {
                    for (Resource resource : resources) {
                        actualResources.add(resource);
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
                }
                return loadCount;
            }
            catch (IOException ex) {
                throw new BeanDefinitionStoreException(
                        "Could not resolve bean definition resource pattern [" + location + "]", ex);
            }
        }
        else {
            // Can only load single resources by absolute URL.
            Resource resource = resourceLoader.getResource(location);
            int loadCount = loadBeanDefinitions(resource);
            if (actualResources != null) {
                actualResources.add(resource);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
            }
            return loadCount;
        }
    }

    @Override
    public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
        Assert.notNull(locations, "Location array must not be null");
        int counter = 0;
        for (String location : locations) {
            counter += loadBeanDefinitions(location);
        }
        return counter;
    }

loadBeanDefinitions(Resource... resources)方法和上年分析的3个方法类似,同样也是调用XmlBeanDefinitionReaderloadBeanDefinitions方法。

从对AbstractBeanDefinitionReaderloadBeanDefinitions方法源码分析可以看出该方法做了一下两件事 首先,调用资源加载器的获取资源方法resourceLoader.getResource(location);,获取要加载的资源。 其次,真正执行加载功能是其子类XmlBeanDefinitionReaderloadBeanDefinitions方法

2.6 资源加载器获取要读入的资源

AbstractBeanDefinitionReader通过调用DefaultResourceLoadergetResource(location);方法获取要加载的资源,其源码如下

代码语言:javascript
复制
/**
 * 获取Resource的具体方法
 */
@Override
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not be null");

        for (ProtocolResolver protocolResolver : this.protocolResolvers) {
            Resource resource = protocolResolver.resolve(location, this);
            if (resource != null) {
                return resource;
            }
        }

        if (location.startsWith("/")) {
            return getResourceByPath(location);
        }
// 如果是类路径(classpath:xx)的话,需要使用ClassPathResource来得到bean文件的资源对象
        else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
        }
        else {
            try {
                // Try to parse the location as a URL...
                // 如果URL方式,使用UrlResource作为bean文件的资源对象
                URL url = new URL(location);
                return new UrlResource(url);
            }
            catch (MalformedURLException ex) {
                // No URL -> resolve as resource path.
                // 如果既不是classpath标识,又不是URL标识的Resource定位,
                // 则调用容器本身的getResourceByPath方法获取Resource
                return getResourceByPath(location);
            }
        }
    }

FileSystemXmlApplicationContext容器提供了getResourceByPath方法,就是为了处理既不是classpath标识,又不是URL标识的Resource定位这种情况

代码语言:javascript
复制
@Override
    protected Resource getResourceByPath(String path) {
        if (path != null && path.startsWith("/")) {
            path = path.substring(1);
        }
        // 这使用文件系统资源对象来定义bean文件
        return new FileSystemResource(path);
    }

这样代码就回到了FileSystemXmlApplicationContext中来,他提供了从FileSystemResource来完成从文件系统中得到配置文件的资源定义

这样,就可以从文件路径上对IOC配置文件进行加载 - 当然我们可以按照这个逻辑从任何地方加载,在Spring中我们看到它提供的各种资源抽象,比如ClassPathResource,URLResource,FileSystemResource等供我们使用。上面我们看到的是定位Resource的一个过程,而这只是加载过程的一部分

至此,我们完成了IOC资源文件的定位

2.8 XmlBeanDefinitionReader加载Bean定义资源

Bean定义的Resource得到了,继续回到XmlBeanDefinitionReader中的loadBeanDefinitions(Resource ...)方法看到代表bean文件的资源定义以后的载入过程

代码语言:javascript
复制
    /**
     * XmlBeanDefinitionReader加载资源的入口方法
     */
    @Override
    public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
                // 将读入的Xml资源进行特殊编码处理
        return loadBeanDefinitions(new EncodedResource(resource));
    }

    /**
     * Load bean definitions from the specified XML file.
     * @param encodedResource the resource descriptor for the XML file,
     * allowing to specify an encoding to use for parsing the file
     * @return the number of bean definitions found
     * @throws BeanDefinitionStoreException in case of loading or parsing errors
     */
    public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
        Assert.notNull(encodedResource, "EncodedResource must not be null");
        if (logger.isInfoEnabled()) {
            logger.info("Loading XML bean definitions from " + encodedResource.getResource());
        }

        Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
        if (currentResources == null) {
            currentResources = new HashSet<EncodedResource>(4);
            this.resourcesCurrentlyBeingLoaded.set(currentResources);
        }
        if (!currentResources.add(encodedResource)) {
            throw new BeanDefinitionStoreException(
                    "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
        }
        try {
            // 将资源文件转换为Input的IO流
            InputStream inputStream = encodedResource.getResource().getInputStream();
            try {
            // 从inputStream中得到XML的解析源
                InputSource inputSource = new InputSource(inputStream);
                if (encodedResource.getEncoding() != null) {
                    inputSource.setEncoding(encodedResource.getEncoding());
                }
            // 这里是具体的读取过程
                return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
            }
            finally {
            // 关闭从Resource中得到的IO流
                inputStream.close();
            }
        }
        catch (IOException ex) {
            throw new BeanDefinitionStoreException(
                    "IOException parsing XML document from " + encodedResource.getResource(), ex);
        }
        finally {
            currentResources.remove(encodedResource);
            if (currentResources.isEmpty()) {
                this.resourcesCurrentlyBeingLoaded.remove();
            }
        }
    }
代码语言:javascript
复制
/**
 * 从特定XML文件中实际载入Bean定义资源的方法
 */
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
            throws BeanDefinitionStoreException {
        try {
            // 将XML文件转换为DOM对象,解析过程由documentReader来完成
            Document doc = doLoadDocument(inputSource, resource);
            // 这里是启动Bean定义资源的详细过程,该过程会用到Spring的bean配置规则
            return registerBeanDefinitions(doc, resource);
        }
        catch (BeanDefinitionStoreException ex) {
            throw ex;
        }
        catch (SAXParseException ex) {
            throw new XmlBeanDefinitionStoreException(resource.getDescription(),
                    "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
        }
        catch (SAXException ex) {
            throw new XmlBeanDefinitionStoreException(resource.getDescription(),
                    "XML document from " + resource + " is invalid", ex);
        }
        catch (ParserConfigurationException ex) {
            throw new BeanDefinitionStoreException(resource.getDescription(),
                    "Parser configuration exception parsing XML from " + resource, ex);
        }
        catch (IOException ex) {
            throw new BeanDefinitionStoreException(resource.getDescription(),
                    "IOException parsing XML document from " + resource, ex);
        }
        catch (Throwable ex) {
            throw new BeanDefinitionStoreException(resource.getDescription(),
                    "Unexpected exception parsing XML document from " + resource, ex);
        }
    }

2.9 DocumentReader将bean定义资源转换为Document对象

DocumentReader将将Bean的定义资源转换成Document对象的源码如下

代码语言:javascript
复制
    /**
     * Load the {@link Document} at the supplied {@link InputSource} using the standard JAXP-configured
     * XML parser.
     * 使用标准的JAXP将载入的Bean定义资源转换成document对象
     */
    @Override
    public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
            ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
        // 创建文件解析器工厂 
        DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
        if (logger.isDebugEnabled()) {
            logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
        }
        // 创建文档解析器 
        DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
        return builder.parse(inputSource);
    }
代码语言:javascript
复制
    /**
     * Create the {@link DocumentBuilderFactory} instance.
     * @param validationMode the type of validation: {@link XmlValidationModeDetector#VALIDATION_DTD DTD}
     * or {@link XmlValidationModeDetector#VALIDATION_XSD XSD})
     * @param namespaceAware whether the returned factory is to provide support for XML namespaces
     * @return the JAXP DocumentBuilderFactory
     * @throws ParserConfigurationException if we failed to build a proper DocumentBuilderFactory
     */
    protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
            throws ParserConfigurationException {
        // 创建文件解析器工厂 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(namespaceAware);
        // 设置解析XML的校验
        if (validationMode != XmlValidationModeDetector.VALIDATION_NONE) {
            factory.setValidating(true);
            if (validationMode == XmlValidationModeDetector.VALIDATION_XSD) {
                // Enforce namespace aware for XSD...
                factory.setNamespaceAware(true);
                try {
                    factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
                }
                catch (IllegalArgumentException ex) {
                    ParserConfigurationException pcex = new ParserConfigurationException(
                            "Unable to validate using XSD: Your JAXP provider [" + factory +
                            "] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? " +
                            "Upgrade to Apache Xerces (or Java 1.5) for full XSD support.");
                    pcex.initCause(ex);
                    throw pcex;
                }
            }
        }

        return factory;
    }

该解析过程调用JavaEE的JAXP标准进行处理。

至此Spring IOC容器根据定位的Bean资源文件,将其加载读入并转换成Document对象过程完成。

接下来我们将继续分析Spring IOC容器将载入的Bean定义资源文件转换为Document之后,是如何将其解析为Spring IOC管理的Bean对象并将其注册到容器中的

2.10 XmlBeanDefinitionReader解析载入的Bean定义资源文件

XmlBeanDefinitionReader类中的doLoadBeanDefinitions方法是从特定的XML文件中实际载入bean定义资源的方法,该方法在载入bean定义资源之后将其转换为Document对象,接下来调用registerBeanDefinitions启动Spring IOC容器对Bean定义的解析过程,registerBeanDefinitions源码如下

代码语言:javascript
复制
    /**
     * Register the bean definitions contained in the given DOM document.
         * 安装Spring的Bean定义要求将Bean定义资源解析为容器内部数据结构
     * Called by {@code loadBeanDefinitions}.
     * <p>Creates a new instance of the parser class and invokes
     * {@code registerBeanDefinitions} on it.
     * @param doc the DOM document
     * @param resource the resource descriptor (for context information)
     * @return the number of bean definitions found
     * @throws BeanDefinitionStoreException in case of parsing errors
     * @see #loadBeanDefinitions
     * @see #setDocumentReaderClass
     * @see BeanDefinitionDocumentReader#registerBeanDefinitions
     */
    public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
        // 得到BeanDefinitionDocumentReader来对XML格式的beanDefinition格式进行解析
        BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
       // 获得容器中注册的bean数量
        int countBefore = getRegistry().getBeanDefinitionCount();
       // 解析过程入口,这里使用了原型模式, BeanDefinitionDocumentReader 只是一个接口
       // 具体的解析过程由实现类DefaultBeanDefinitionDocumentReader完成
        documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
       // 统计解析的bean数量
        return getRegistry().getBeanDefinitionCount() - countBefore;
    }

Bean定义资源的载入解析主要分为以下两个过程: 1.首先,通过调用XML解析器将Bean定义资源文件转换得到Document对象,但是这些对象并没有按照Spring的bean规则进行解析,这一步是载入的过程 2.其次,在完成通用的XML解析之后,按照Spring的bean规则对Document对象进行解析

照Spring的bean规则对Document对象的过程是接口BeanDefinitionDocumentReader的实现类DefaultBeanDefinitionDocumentReader中实现的

2.11 DefaultBeanDefinitionDocumentReader对bean定义的Document对象解析:

BeanDefinitionDocumentReader接口通过调用其实现类DefaultBeanDefinitionDocumentReaderDocument对象进行解析,解析的代码如下

代码语言:javascript
复制
/**
 * 根据Spring DTD 的bean定义规则解析Bean定义Document对象
 */
    @Override
    public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
        // 获得XML描述符
        this.readerContext = readerContext;
        logger.debug("Loading bean definitions");
        // 获得Document的根元素
        Element root = doc.getDocumentElement();
        // 调用 doRegisterBeanDefinitions 实现具体的解析过程
        doRegisterBeanDefinitions(root);
    }
    /**
     * Register each bean definition within the given root {@code <beans/>} element.
     */
    protected void doRegisterBeanDefinitions(Element root) {
        // Any nested <beans> elements will cause recursion in this method. In
        // order to propagate and preserve <beans> default-* attributes correctly,
        // keep track of the current (parent) delegate, which may be null. Create
        // the new (child) delegate with a reference to the parent for fallback purposes,
        // then ultimately reset this.delegate back to its original (parent) reference.
        // this behavior emulates a stack of delegates without actually necessitating one.
        // 具体的解析过程 由BeanDefinitionParserDelegate实现,这里用到了委派模式
       // BeanDefinitionParserDelegate 定义了Spring定义XML文件的各种元素
        BeanDefinitionParserDelegate parent = this.delegate;
        this.delegate = createDelegate(getReaderContext(), root, parent);

        if (this.delegate.isDefaultNamespace(root)) {
            String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
            if (StringUtils.hasText(profileSpec)) {
                String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
                        profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
                if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
                    if (logger.isInfoEnabled()) {
                        logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
                                "] not matching: " + getReaderContext().getResource());
                    }
                    return;
                }
            }
        }
        // 在解析Bean定义之前,进行自定义的解析,增强解析过程的可扩展性 
        preProcessXml(root);
        // 从Document根元素开始,进行自Bean定义的Document解析
        parseBeanDefinitions(root, this.delegate);
        // 在解析Bean定义之后,进行自定义的解析,增强解析过程的可扩展性 
        postProcessXml(root);
        // 为递归解析子元素做准备
        this.delegate = parent;
    }
    /**
     * 创建BeanDefinitionParserDelegate对象,用于真正的解析过程
     */
    protected BeanDefinitionParserDelegate createDelegate(
            XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
        BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
   // 从BeanDefinitionParserDelegate初始化 Document根元素
        delegate.initDefaults(root, parentDelegate);
        return delegate;
    }
代码语言:javascript
复制
    /**
     * Parse the elements at the root level in the document:
     * "import", "alias", "bean".
     * @param root the DOM root element of the document
     * 使用Spring的Bean定义规则从Document的根元素开始解析bean定义的Document对象
     */
    protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
      //Bean定义的Document对象使用了Spring默认的XML命名空间
        if (delegate.isDefaultNamespace(root)) {
     //从Bean定义的Document对象获取根元素的所有子节点
            NodeList nl = root.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                Node node = nl.item(i);
     // 获取Document节点是XML元素节点
                if (node instanceof Element) {
                    Element ele = (Element) node;
     // Bean定义的Document的元素节点使用的是Spring默认的XML命名空间
                    if (delegate.isDefaultNamespace(ele)) {
     // 使用Spring的bean解析元素节点
                        parseDefaultElement(ele, delegate);
                    }
                    else {
     // 没有使用Spring默认的XML命名空间,则使用用户自定义的解析规则解析元素节点
                        delegate.parseCustomElement(ele);
                    }
                }
            }
        }
        else {
     // 没有使用Spring默认的XML命名空间,则使用用户自定义的解析规则解析元素节点
            delegate.parseCustomElement(root);
        }
    }
代码语言:javascript
复制
/**
 * 使用Spring的Bean规则解析Document元素节点
 */
    private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
        // 如果元素节点是<Import> 导入元素,进行导入解析
        if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
            importBeanDefinitionResource(ele);
        }
        // 如果元素节点是<Alias> 导入元素,进行导入解析
        else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
            processAliasRegistration(ele);
        }
       // 元素节点既不是导入元素,也不是别名元素,即普通的<Bean>元素
       // 安装Spring的Bean规则解析元素
        else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
            processBeanDefinition(ele, delegate);
        }
        else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
            // recurse
            doRegisterBeanDefinitions(ele);
        }
    }
代码语言:javascript
复制
    /**
     * Parse an "import" element and load the bean definitions
     * from the given resource into the bean factory.
     * 解析导入的<Import>元素,从给定的导入路径加载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;
        }

        // Resolve system properties: e.g. "${user.dir}"
        // 使用系统变量值解析location属性值
        location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);

        Set<Resource> actualResources = new LinkedHashSet<Resource>(4);

        // Discover whether the location is an absolute or relative URI
        // 标示给定的导入元素的location是否是绝对路径
        boolean absoluteLocation = false;
        try {
            absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
        }
        catch (URISyntaxException ex) {
            // cannot convert to an URI, considering the location relative
            // unless it is the well-known Spring prefix "classpath*:"
        }

        // Absolute or relative?
        // 给定的导入元素的location是绝对路径
        if (absoluteLocation) {
            try {
        // 使用资源读入加载器加载给定路径的bean定义资源,(这里又递归调用了loadBeanDefinitions函数)
                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是相对路径
            // No URL -> considering resource location as relative to the current file.
            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()]);
        // 在解析完<Import>元素之后,发送容器导入其他资源处理完成事件
        getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
    }
代码语言:javascript
复制
    /**
     * Process the given alias element, registering the alias with the registry.
         * 解析<Alias>别名元素,为Bean向Spring IOC容器注册别名
     */
    protected void processAliasRegistration(Element ele) {
        String name = ele.getAttribute(NAME_ATTRIBUTE);
        String alias = ele.getAttribute(ALIAS_ATTRIBUTE);
        boolean valid = true;
        if (!StringUtils.hasText(name)) {
            getReaderContext().error("Name must not be empty", ele);
            valid = false;
        }
        if (!StringUtils.hasText(alias)) {
            getReaderContext().error("Alias must not be empty", ele);
            valid = false;
        }
        if (valid) {
            try {
                getReaderContext().getRegistry().registerAlias(name, alias);
            }
            catch (Exception ex) {
                getReaderContext().error("Failed to register alias '" + alias +
                        "' for bean with name '" + name + "'", ele, ex);
            }
            getReaderContext().fireAliasRegistered(name, alias, extractSource(ele));
        }
    }

通过上述Spring IOC容器对载入的Bean定义Document解析可以看出,我们使用Spring时,在Spring配置文件中可以使用<Import>元素来导入IOC容器所需要的其他资源,Spring IOC容器在解析时会首先指定导入的资源加载进容器中。使用<Alias>别名时,Spring IOC容器首先将别名元素所定义的别名注册到容器中。

对于既不是<Import>也不是<Alias>的元素,即Spring配置文件中普通<Bean>元素的解析由BeanDefinitionParserDelegate类的parseBeanDefinitionElement方法来实现。

2.12BeanDefinitionParserDelegate解析<Bean>定义资源文件的<Bean>元素

对于既不是<Import>也不是<Alias>的元素,即Spring配置文件中普通<Bean>元素的解析由BeanDefinitionParserDelegate类的parseBeanDefinitionElement方法来实现。源码如下:

代码语言:javascript
复制
    /**
     * Process the given bean element, parsing the bean definition
     * and registering it with the registry.
     */
    protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
        // BeanDefinitionHolder是对BeanDefinition的封装,即Bean定义的封装
        // 对<Bean>元素的解析由BeanDefinitionParserDelegate实现
        BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
        if (bdHolder != null) {
            bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
            try {
                // Register the final decorated instance.
          // 向Spring IOC容器注册解析得到的Bean定义,这是Bean定义向IOC容器注册的入口
                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));
        }
    }
代码语言:javascript
复制
    // 解析<Bean>元素的入口
    public BeanDefinitionHolder parseBeanDefinitionElement(Element ele) {
        return parseBeanDefinitionElement(ele, null);
    }
  // 解析<Bean>元素,这个方法主要处理id,name和别名属性
    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>();
// 将<Bean>元素中的所有name属性值存放到别名中
        if (StringUtils.hasLength(nameAttr)) {
            String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS);
            aliases.addAll(Arrays.asList(nameArr));
        }

        String beanName = id;
// 如果<Bean>元素中没有id,则将别名中第一个元素复制给beanName
        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");
            }
        }
// 检测<Bean>中id和name的唯一性,containingBean标示<Bean>元素中是否包含子<Bean>元素
        if (containingBean == null) {
            checkNameUniqueness(beanName, aliases, ele);
        }
// 详细对<Bean>元素中配置的Bean定义进行解析的地方
        AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);
        if (beanDefinition != null) {

            if (!StringUtils.hasText(beanName)) {
                try {
// 如果<Bean>元素中没有配置id,name,或者别名,且没有包含子元素,则为解析的Bean生成一个唯一的beanName并注册
                    if (containingBean != null) {
                        beanName = BeanDefinitionReaderUtils.generateBeanName(
                                beanDefinition, this.readerContext.getRegistry(), true);
                    }
                    else {
// 如果有id或者name,或者别名子元素,则使用悲鸣进行注册
                        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.
//  Spring 1.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);
        }

        return null;
    }
代码语言:javascript
复制
    /**
     * Parse the bean definition itself, without regard to name or aliases. May return
     * {@code null} if problems occurred during the parsing of the bean definition.
     * 详细对<Bean>元素中配置的Bean定义其他元素进行解析,由于上面方法已经对id,name,别名,子元素进行了解析,这里只对其他元素进行解析
     */
    public AbstractBeanDefinition parseBeanDefinitionElement(
            Element ele, String beanName, BeanDefinition containingBean) {

// 记录解析的bean
        this.parseState.push(new BeanEntry(beanName));

// 这里只读取<Bean>元素中配置的Class名字,然后载入到BeanDefinition中,不做实例化,实例化在依赖注入过程中完成
        String className = null;
        if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
            className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
        }

        try {
// 如果<Bean>元素中配置了parent值,则获取parent属性的值
            String parent = null;
            if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
                parent = ele.getAttribute(PARENT_ATTRIBUTE);
            }
// 根据<Bean> 元素配置的class名称和parent属性创建BeanDefinition,为载入Bean定义信息做准备
            AbstractBeanDefinition bd = createBeanDefinition(className, parent);

// 对当前<Bean>元素中配置的一些属性进行解析和配置,如单例属性等
            parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
            
// 解析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);
// 解析<Propterty>属性
            parsePropertyElements(ele, bd);
// 解析<Qualifier>属性
            parseQualifierElements(ele, 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;
    }

只要使用过Spring,对Spring配置文件比较熟悉的人,通过对上述源码的分析,就会明白我们在Spring配置文件中<Bean>元素的中配置的属性就是通过该方法解析和设置到Bean中的

注意:这里只是将信息解析放入BeanDefinition中,并没有实例化,实例化的过程是在依赖注入中,客户端第一次像容器索要Bean的时候发生的

下面我们继续分析。了解bean的属性是如何设置的

2.13 BeanDefinitionParserDelegate解析<Property>属性

BeanDefinitionParserDelegate在解析<Bean>是调用 parsePropertyElements方法解析<Bean>中<property>元素子元素,解析源码如下:

代码语言:javascript
复制
    public void parsePropertyElements(Element beanEle, BeanDefinition bd) {
        NodeList nl = beanEle.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (isCandidateElement(node) && nodeNameEquals(node, PROPERTY_ELEMENT)) {
                parsePropertyElement((Element) node, bd);
            }
        }
    }

    /**
     * Parse a property element.
     */
    public void parsePropertyElement(Element ele, BeanDefinition bd) {
        String propertyName = ele.getAttribute(NAME_ATTRIBUTE);
        if (!StringUtils.hasLength(propertyName)) {
            error("Tag 'property' must have a 'name' attribute", ele);
            return;
        }
        this.parseState.push(new PropertyEntry(propertyName));
        try {
            // 如果已经有同样名字的属性被解析,则不重复解析
           // 即如果在一个Bean中配置同名的property属性,只会取第一个
            if (bd.getPropertyValues().contains(propertyName)) {
                error("Multiple 'property' definitions for property '" + propertyName + "'", ele);
                return;
            }
            // 解析Property值
            Object val = parsePropertyValue(ele, bd, propertyName);
           // 根据Property的名字和值创建PropertyValue实例
            PropertyValue pv = new PropertyValue(propertyName, val);
          // 解析<property元素中的属性> 
            parseMetaElements(ele, pv);
            pv.setSource(extractSource(ele));
            bd.getPropertyValues().addPropertyValue(pv);
        }
        finally {
            this.parseState.pop();
        }
    }
代码语言:javascript
复制
    /**
     * Get the value of a property element. May be a list etc.
     * Also used for constructor arguments, "propertyName" being null in this case.
     */
    public Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {
        String elementName = (propertyName != null) ?
                        "<property> element for property '" + propertyName + "'" :
                        "<constructor-arg> element";

        // Should only have one child element: ref, value, list, etc.
// 获取所有子元素,只能是一种类型:ref, value, list等
        NodeList nl = ele.getChildNodes();
        Element subElement = null;
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
// 子元素不是description和meta属性
            if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT) &&
                    !nodeNameEquals(node, META_ELEMENT)) {
                // Child element is what we're looking for.
                if (subElement != null) {
                    error(elementName + " must not contain more than one sub-element", ele);
                }
                else {
                    subElement = (Element) node;
                }
            }
        }

        boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE);
        boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE);
        if ((hasRefAttribute && hasValueAttribute) ||
                ((hasRefAttribute || hasValueAttribute) && subElement != null)) {
            error(elementName +
                    " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element", ele);
        }

// 如果属性是ref,创建一个ref的数据对象RuntimeBeanReference
// 这个对象封装了ref信息
        if (hasRefAttribute) {
            String refName = ele.getAttribute(REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error(elementName + " contains empty 'ref' attribute", ele);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(ele));
            return ref;
        }
// 如果是value信息
        else if (hasValueAttribute) {
            TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));
            valueHolder.setSource(extractSource(ele));
            return valueHolder;
        }
        else if (subElement != null) {
            return parsePropertySubElement(subElement, bd);
        }
        else {
            // Neither child element nor "ref" or "value" attribute found.
            error(elementName + " must specify a ref or value", ele);
            return null;
        }
    }

通过对上述源码分析,我们可以了解在Spring配置文件中,<Bean>元素中<property>元素的相关配置是如何处理的

  1. ref 被封装为指向一个对象的引用 2.value被封装为一个字符串类型的对象 3.ref和value都通过“解析的数据类型值”.setSource()"将属性值/引用与所引用的属性关联起来

该方法最后对于<property>的子元素通过进行解析parsePropertySubElement方法进行解析,我们继续分析改源码

2.15 解析<property>的子元素

代码语言:javascript
复制
    public Object parsePropertySubElement(Element ele, BeanDefinition bd) {
        return parsePropertySubElement(ele, bd, null);
    }

    /**
     * Parse a value, ref or collection sub-element of a property or
     * constructor-arg element.
     * @param ele subelement of property element; we don't know which yet
     * @param defaultValueType the default type (class name) for any
     * {@code <value>} tag that might be created
     */
    public Object parsePropertySubElement(Element ele, BeanDefinition bd, String defaultValueType) {
        if (!isDefaultNamespace(ele)) {
            return parseNestedCustomElement(ele, bd);
        }
        else if (nodeNameEquals(ele, BEAN_ELEMENT)) {
            BeanDefinitionHolder nestedBd = parseBeanDefinitionElement(ele, bd);
            if (nestedBd != null) {
                nestedBd = decorateBeanDefinitionIfRequired(ele, nestedBd, bd);
            }
            return nestedBd;
        }
        else if (nodeNameEquals(ele, REF_ELEMENT)) {
            // A generic reference to any name of any bean.
            String refName = ele.getAttribute(BEAN_REF_ATTRIBUTE);
            boolean toParent = false;
            if (!StringUtils.hasLength(refName)) {
                // A reference to the id of another bean in the same XML file.
                refName = ele.getAttribute(LOCAL_REF_ATTRIBUTE);
                if (!StringUtils.hasLength(refName)) {
                    // A reference to the id of another bean in a parent context.
                    refName = ele.getAttribute(PARENT_REF_ATTRIBUTE);
                    toParent = true;
                    if (!StringUtils.hasLength(refName)) {
                        error("'bean', 'local' or 'parent' is required for <ref> element", ele);
                        return null;
                    }
                }
            }
            if (!StringUtils.hasText(refName)) {
                error("<ref> element contains empty target attribute", ele);
                return null;
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName, toParent);
            ref.setSource(extractSource(ele));
            return ref;
        }
        else if (nodeNameEquals(ele, IDREF_ELEMENT)) {
            return parseIdRefElement(ele);
        }
        else if (nodeNameEquals(ele, VALUE_ELEMENT)) {
            return parseValueElement(ele, defaultValueType);
        }
        else if (nodeNameEquals(ele, NULL_ELEMENT)) {
            // It's a distinguished null value. Let's wrap it in a TypedStringValue
            // object in order to preserve the source location.
            TypedStringValue nullHolder = new TypedStringValue(null);
            nullHolder.setSource(extractSource(ele));
            return nullHolder;
        }
        else if (nodeNameEquals(ele, ARRAY_ELEMENT)) {
            return parseArrayElement(ele, bd);
        }
        else if (nodeNameEquals(ele, LIST_ELEMENT)) {
            return parseListElement(ele, bd);
        }
        else if (nodeNameEquals(ele, SET_ELEMENT)) {
            return parseSetElement(ele, bd);
        }
        else if (nodeNameEquals(ele, MAP_ELEMENT)) {
            return parseMapElement(ele, bd);
        }
        else if (nodeNameEquals(ele, PROPS_ELEMENT)) {
            return parsePropsElement(ele);
        }
        else {
            error("Unknown property sub-element: [" + ele.getNodeName() + "]", ele);
            return null;
        }
    }

通过上述源码分析,我们明白了在Spring配置文件中,对<Property>元素中配置的list,array,set,map,prop等各种子元素集合都通过上述方法解析,生成对应的数据对象

经过对Spring Bean定义资源文件转换的Document对象中的元素层层解析,Spring IOC现在已经将XML形式定义的Bean定义资源文件转换为Spring IOC所识别的数据结构--BeanDefinition,他是Bean定义资源文件配置的POJO对象在Spring IOC容器中的映射,我们可以通过AbstractBeanDefinition为入口,看到了IOC容器进行索引,查询和操作

经过Spring IOC容器对Bean定义资源文件的解析后,IOC容器大致完成了管理Bean对象的准备工作,即初始化过程,但是最为重要的依赖注入还没有发生。现在在IOC容器中的BeanDefinition存储的只是一些静态信息,接下来需要向容器注册Bean定义信息才能全部完成IOC容器的初始化过程

2.16 解析过后的BeanDefinition在IOC容器中的注册:

让我们继续跟踪程序的执行顺序,接下来我们回到2.12中分析DefaultBeanDefinitionDocumentReader对Bean定义转换的Document对象解析的流程中,在其pareBeanDefaultElement方法中完成对Document对象的解析后得到的BeanDefinition对象,然后调用BeanDefinitionUtilsregisterBeanDefinition方法向IOC容器注册解析的bean,BeanDefinitionUtils的源码如下:

代码语言:javascript
复制
    /**
     * Register the given bean definition with the given bean factory.
     * 向给定的beanFactory注册给定的bean
     * @param definitionHolder the bean definition including name and aliases
     * @param registry the bean factory to register with
     * @throws BeanDefinitionStoreException if registration failed
     */
    public static void registerBeanDefinition(
            BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
            throws BeanDefinitionStoreException {

        // Register bean definition under primary name.
        String beanName = definitionHolder.getBeanName();
// 这里主要是使用了BeanDefinitionRegistry的registerBeanDefinition方法实现
// 注意这里的BeanDefinitionRegistry实际上是我们的FileSystemXmlBeanFactory,
// 因为其继承了DefaultListableBeanFactory,进而实现了BeanDefinitionRegistry接口
        registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

        // Register aliases for bean name, if any.
        String[] aliases = definitionHolder.getAliases();
        if (aliases != null) {
            for (String alias : aliases) {
                registry.registerAlias(beanName, alias);
            }
        }
    }
代码语言:javascript
复制
    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
            throws BeanDefinitionStoreException {

        Assert.hasText(beanName, "Bean name must not be empty");
        Assert.notNull(beanDefinition, "BeanDefinition must not be null");

// 校验解析的bean
        if (beanDefinition instanceof AbstractBeanDefinition) {
            try {
                ((AbstractBeanDefinition) beanDefinition).validate();
            }
            catch (BeanDefinitionValidationException ex) {
                throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
                        "Validation of bean definition failed", ex);
            }
        }

        BeanDefinition oldBeanDefinition;


        oldBeanDefinition = this.beanDefinitionMap.get(beanName);
        if (oldBeanDefinition != null) {
//检查是否有同名的bean已经注册,如果有,并且不允许覆盖已注册的bean,则抛出异常
            if (!isAllowBeanDefinitionOverriding()) {
                throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
                        "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
                        "': There is already [" + oldBeanDefinition + "] bound.");
            }
// 如果允许覆盖已注册的bean,则新的覆盖旧的bean
            else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {
                // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +
                            "' with a framework-generated bean definition: replacing [" +
                            oldBeanDefinition + "] with [" + beanDefinition + "]");
                }
            }
            else if (!beanDefinition.equals(oldBeanDefinition)) {
                if (this.logger.isInfoEnabled()) {
                    this.logger.info("Overriding bean definition for bean '" + beanName +
                            "' with a different definition: replacing [" + oldBeanDefinition +
                            "] with [" + beanDefinition + "]");
                }
            }
            else {
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Overriding bean definition for bean '" + beanName +
                            "' with an equivalent definition: replacing [" + oldBeanDefinition +
                            "] with [" + beanDefinition + "]");
                }
            }
            this.beanDefinitionMap.put(beanName, beanDefinition);
        }
//如果没有同名的已经注册的bean,则按正常流程注册
        else {
            if (hasBeanCreationStarted()) {
                // Cannot modify startup-time collection elements anymore (for stable iteration)
                synchronized (this.beanDefinitionMap) {
                    this.beanDefinitionMap.put(beanName, beanDefinition);
                    List<String> updatedDefinitions = new ArrayList<String>(this.beanDefinitionNames.size() + 1);
                    updatedDefinitions.addAll(this.beanDefinitionNames);
                    updatedDefinitions.add(beanName);
                    this.beanDefinitionNames = updatedDefinitions;
                    if (this.manualSingletonNames.contains(beanName)) {
                        Set<String> updatedSingletons = new LinkedHashSet<String>(this.manualSingletonNames);
                        updatedSingletons.remove(beanName);
                        this.manualSingletonNames = updatedSingletons;
                    }
                }
            }
            else {
                // Still in startup registration phase
                this.beanDefinitionMap.put(beanName, beanDefinition);
                this.beanDefinitionNames.add(beanName);
                this.manualSingletonNames.remove(beanName);
            }
            this.frozenBeanDefinitionNames = null;
        }

        if (oldBeanDefinition != null || containsSingleton(beanName)) {
// 重置所有已经注册的bean
            resetBeanDefinition(beanName);
        }
    }

从注册的代码中可以发现,实际上,IOC容器是通过维护了一个ConcurrentHashMap来保存bean的定义信息,其中key为bean的名字,value为BeanDefinition对象。

至此,Bean定义资源文件中配置的Bean被解析后,已经注册到IOC容器中,被容器管理起来。现在IOC容器中已经建立了整个Bean的配置信息,这些BeanDefinition信息已经可以使用,可以被检索,IOC容器的作用就是对这些注册的Bean定义信息进行注册和维护。这些注册的bean定义信息是IOC容器控制反转的基础,正是有了这些注册的数据,容器才可以进行依赖注入


3 总结

通过上面的代码分析,总结一下IOC容器初始化的基本步骤:

1.初始化的入口在容器中实现的refresh()中来完成。 2.对bean定义载入的IOC容器使用的方法是loadBeanDefinitions

其中大致的过程就是:通过ResourceLoader来完成资源文件位置的定位,DefaultResourceLoader是默认的实现,同时上下文本身就给出了ResourceLoader的实现,可以从类路径,文件系统,URL等方式来定位资源。如果是XmlBeanFactory作为IOC容器,那么需要为他指定bean定义的资源,也就是说bean定义文件时通过抽象成Resource来被IOC容器处理的,容器通过BeanDefinitionReader来完成定义信息的解析和注册。往往使用的是XmlBeanDefinitionReader来解析bean的XML文件定义文件-实际的处理过程是委托给BeanDefinitionParserDelegate来完成的,从而得到bean的定义信息,这些信息在Spring中使用BeanDefinition对象来表示--这个名字可以让我们想到loadBeanDefinition,registerBeanDefinition这些相关方法,他们都是为处理BeanDefinition服务的,容器解析到BeanDefinition以后,需要把他在IOC容器中注册,这由IOC实现BeanDefinitionRegistry接口来实现,注册过程就是IOC容器内部维护一个HashMap来保存得到的BeanDefinition的过程,这个HashMap是IOC容器持有bean信息的场所,以后对bean的操作都是围绕这个hashmap来完成的

3.然后我们就可以通过BeanFactory和ApplicationContext来享受到SpringIOC的服务了。在使用IOC容器的时候,我们注意到除了少量粘贴代码,绝大多数以正确IOC风格编程的应用代码完成不用关心如何到达工厂,因为容器将把这些对象与容器管理的其他对象勾在一起。基本的策略是把工厂放到已知的地方,最好是放在预期的上线文有意义的地方,以及代码将实际访问工厂的地方。Spring本身对声明式载入Web程序的应用程序上下文,并将其存储在ServletContext中的框架来实现。

在使用SpringI0C 容器的时候我们还需要区别两个概念:BeanFactory 和FactoryBean

其中BeanFactory指的是IOC 容器的编程抽象,比如ApplicationConitext,XmlBeanFactory等,这些都是IOC 容器的具体表现,需要使用什么样的容器由客户决定,但Spring 为我们提供了丰富的选择。FactoryBean 只是一个可以在IOC 而容器中被管理的一个bean,是对各种处理过程和资源使用的抽象,FactoryBean在需要时产生另一个对象,而不返回FactoryBean 本身,我们可以把它看成是一个抽象工厂,对它的调用返回的是工厂生产的产品。所有的FactoryBean 都实现特殊的 org.springframework.beans.factory.FactoryBean接口,当使用容器中的FactoryBean的时候,该容器不会返回FactoryBean.本身,而是返回其生成的对象。Spring 包括了大部分的通用资源和服务访问抽象的FactoryBean.的实现,其中包括:对JNDI 查询的处理,对代理对象的处理,对事务性代理的处理,对RMI 代理的处理等,这些我们都可以看成是具体的工厂,看成是Spring 为我们建立好的工厂。也就是说Spring 通过使用抽象工厂模式为我们准备了一系列工厂来生产一些特定的对象,免除我们手工重复的工作,我们要使用时只需要在IOC 容器里配置好就能很方便的使用了

最后,总结一张方法调用图,暂时比较丑陋,后面补上正规的方法调用图

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.05.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.Spring的几个核心类
    • 1.1.BeanFactory
      • 1.2 BeanDefinition
        • 1.3 BeanDefinitionReader
          • 2.1 高富帅IOC解剖
          • 2.2设置资源加载器和资源定位
          • 2.3 AbstractApplicationContext的refresh函数载入Bean定义过程
          • 2.4 具体的refreshBeanFactory方法执行过程
          • 2.5 AbstractRefreshableApplicationContext子类的loadBeanDefinitions方法
          • 2.6 资源加载器获取要读入的资源
          • 2.8 XmlBeanDefinitionReader加载Bean定义资源
          • 2.9 DocumentReader将bean定义资源转换为Document对象
          • 2.10 XmlBeanDefinitionReader解析载入的Bean定义资源文件
          • 2.11 DefaultBeanDefinitionDocumentReader对bean定义的Document对象解析:
          • 2.12BeanDefinitionParserDelegate解析<Bean>定义资源文件的<Bean>元素
          • 2.13 BeanDefinitionParserDelegate解析<Property>属性
          • 2.15 解析<property>的子元素
          • 2.16 解析过后的BeanDefinition在IOC容器中的注册:
          • 通过上面的代码分析,总结一下IOC容器初始化的基本步骤:
      • 2.IOC容器的初始化
      • 3 总结
        • 在使用SpringI0C 容器的时候我们还需要区别两个概念:BeanFactory 和FactoryBean
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档