Spring源碼系列--1. Web IOC 容器(上)

BeanFactory

Spring Bean 的創建是典型的工廠模式,這一系列的 Bean 工廠,即 IOC 容器為開發者管理對象
間的依賴關系提供了很多便利和基礎服務,在 Spring 中有許多的 IOC 容器的實現供用戶選擇和使用,
其相互關系如下:


image.png

其中 BeanFactory 作為最頂層的一個接口類,它定義了 IOC 容器的基本功能規范,BeanFactory 有三個重要的子類:ListableBeanFactory、HierarchicalBeanFactory 、AutowireCapableBeanFactory
但是從類圖中我們可以發現最終的默認實現類是 DefaultListableBeanFactory,它實現了所有的接口

那為何要定義這么多層次的接口呢?

查閱這些接口的源碼和說明發現,每個接口都有它使用的場合,它主要是為了區分在 Spring 內部在操作過程中對象的傳遞和轉化過程時,對對象的數據訪問所做的限制
例如:

  • ListableBeanFactory 接口表示這些 Bean 是可列表化的
  • HierarchicalBeanFactory 表示的是這些 Bean 是有繼承關系的,也就是每個 Bean 有可能有父 Bean
  • AutowireCapableBeanFactory 接口定義 Bean 的自動裝配規則
    這三個接口共同定義了 Bean 的集合、Bean 之間的關系、以及 Bean 行為

我們來看下最基本的 IOC 容器接口 BeanFactory的源碼

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.core.ResolvableType;

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, @Nullable Class<T> requiredType) throws BeansException;

    Object getBean(String name, Object... args) throws BeansException;

    <T> T getBean(Class<T> requiredType) throws BeansException;

    <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;

    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

    boolean isTypeMatch(String name, ResolvableType typeToMatch) throws
            NoSuchBeanDefinitionException;

    boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws
            NoSuchBeanDefinitionException;

    //得到 bean 實例的 Class 類型
    @Nullable
    Class<?> getType(String name) throws NoSuchBeanDefinitionException;

    //得到 bean 的別名,如果根據別名檢索,那么其原名也會被檢索出來
    String[] getAliases(String name);
}

從 BeanFactory 里的源碼可知,它只是一個接口,只對 IOC 容器的基本行為作了定義,至于Bean 是如何定義怎樣加載的并不關心,想知道BeanFactory 如何產生對象,需要看具體的IOC容器的實現類,比如GenericApplicationContext , ClasspathXmlApplicationContext等


image.png

ApplicationContext 是 Spring 提供的一個高級的 IOC 容器,它除了能夠提供 IOC 容器的基本功能
外,還為用戶提供了以下的附加服務。從 ApplicationContext 接口的實現,我們看出其特點:

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
        MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

1、支持信息源,可以實現國際化(實現 MessageSource 接口)
2、訪問資源(實現 ResourcePatternResolver 接口)
3、支持應用事件(實現 ApplicationEventPublisher 接口)

BeanDefinition

SpringIOC 容器管理了我們定義的各種 Bean 對象及其相互的關系,Bean 對象在 Spring 實現中是
以 BeanDefinition 來描述的,其繼承體系如下:


image.png

BeanDefinitionReader

Bean 的解析過程非常復雜,功能被分的很細,因為這里需要被擴展的地方很多,必須保證有足夠的靈活性,以應對可能的變化。Bean 的解析主要就是對 Spring 配置文件的解析。這個解析過程主要通過BeanDefinitionReader 來完成,最后看看 Spring 中 BeanDefinitionReader 的類結構圖:


image.png

簡單描述一下 Spring 解析流程:
1、解析 applicationgContext.xml
2、將 xml 中定義的 bean(如 <bean id="userService" class="com.xxx.UserServiceImpl">) 解析成 Spring 內部的 BeanDefinition
3、以 beanName(如 userService) 為 key,BeanDefinition(如 UserServiceImpl) 為 value 存儲到 DefaultListableBeanFactory 中的 beanDefinitionMap (其實就是一個 ConcurrentHashMap) 中

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
    /** Map of bean definition objects, keyed by bean name */
    //存儲注冊信息的BeanDefinition
    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
    /** List of bean definition names, in registration order */
    private volatile List<String> beanDefinitionNames = new ArrayList<>(256);

4、同時將 beanName 存入 beanDefinitionNames(List 類型) 中,然后遍歷 beanDefinitionNames 中的 beanName,進行 bean 的實例化并填充屬性,在實例化的過程中,如果有依賴沒有被實例化將先實例化其依賴,然后實例化本身,實例化完成后將實例存入單例 bean 的緩存中,當調用 getBean 方法時,到單例 bean 的緩存中查找,如果找到并經過轉換后返回這個實例 (如 userService的實例),之后就可以直接使用了
大致了解下,后續會詳細跟進代碼詳情

Web IOC 容器

從最熟悉的 DispatcherServlet 開始,最先想到的還是 DispatcherServlet 的 init()方法。發現在 DispatherServlet 中并沒有找到 init()方法。但是往上追索在其父類的父類HttpServletBean 中找到了init()方法

    @Override
    public final void init() throws ServletException {
        if (logger.isDebugEnabled()) {
            logger.debug("Initializing servlet '" + getServletName() + "'");
        }

        // Set bean properties from init parameters.
        PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
        if (!pvs.isEmpty()) {
            try {
                //定位資源
                BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
                //加載配置信息
                ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
                bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
                initBeanWrapper(bw);
                bw.setPropertyValues(pvs, true);
            }
            catch (BeansException ex) {
                if (logger.isErrorEnabled()) {
                    logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
                }
                throw ex;
            }
        }

        // Let subclasses do whatever initialization they like.
        //真正完成初始化容器動作的邏輯在initServletBean方法中
        initServletBean();

        if (logger.isDebugEnabled()) {
            logger.debug("Servlet '" + getServletName() + "' configured successfully");
        }
    }

在 init()方法中,真正完成初始化容器動作的邏輯其實在 initServletBean()方法中,我們繼續跟進
initServletBean()中的代碼在其子類 FrameworkServlet 類中

@Override
    protected final void initServletBean() throws ServletException {
        getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
        if (this.logger.isInfoEnabled()) {
            this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
        }
        long startTime = System.currentTimeMillis();

        try {
            //看這個方法initWebApplicationContext
            this.webApplicationContext = initWebApplicationContext();
            initFrameworkServlet();
        }
        catch (ServletException ex) {
            this.logger.error("Context initialization failed", ex);
            throw ex;
        }
        catch (RuntimeException ex) {
            this.logger.error("Context initialization failed", ex);
            throw ex;
        }

        if (this.logger.isInfoEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
                    elapsedTime + " ms");
        }
    }

繼續跟進 initWebAppplicationContext()方法

protected WebApplicationContext initWebApplicationContext() {
        //先從 ServletContext 中獲得父容器 WebAppliationContext
        WebApplicationContext rootContext =
                WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        //聲明子容器
        WebApplicationContext wac = null;

        //建立父、子容器之間的關聯關系
        if (this.webApplicationContext != null) {
            // A context instance was injected at construction time -> use it
            wac = this.webApplicationContext;
            if (wac instanceof ConfigurableWebApplicationContext) {
                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
                if (!cwac.isActive()) {
                    // The context has not yet been refreshed -> provide services such as
                    // setting the parent context, setting the application context id, etc
                    if (cwac.getParent() == null) {
                        // The context instance was injected without an explicit parent -> set
                        // the root application context (if any; may be null) as the parent
                        cwac.setParent(rootContext);
                    }
                    configureAndRefreshWebApplicationContext(cwac);
                }
            }
        }
        //先去 ServletContext 中查找 Web 容器的引用是否存在,并創建好默認的空 IOC 容器
        if (wac == null) {
            // No context instance was injected at construction time -> see if one
            // has been registered in the servlet context. If one exists, it is assumed
            // that the parent context (if any) has already been set and that the
            // user has performed any initialization such as setting the context id
            wac = findWebApplicationContext();
        }
        //給上一步創建好的 IOC 容器賦值
        if (wac == null) {
            // No context instance is defined for this servlet -> create a local one
            wac = createWebApplicationContext(rootContext);
        }
        //觸發 onRefresh 方法
        if (!this.refreshEventReceived) {
            // Either the context is not a ConfigurableApplicationContext with refresh
            // support or the context injected at construction time had already been
            // refreshed -> trigger initial onRefresh manually here.
            onRefresh(wac);//最終還是調用子類DispatcherServlet的onRefresh方法
        }

        if (this.publishContext) {
            // Publish the context as a servlet context attribute.
            String attrName = getServletContextAttributeName();
            getServletContext().setAttribute(attrName, wac);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
                        "' as ServletContext attribute with name [" + attrName + "]");
            }
        }

        return wac;
    }

最后調用了DispatcherServlet 的 onRefresh()方法,在 onRefresh()方法中又是直接調用 initStrategies()方法初始化 SpringMVC 的九大組件

@Override
    protected void onRefresh(ApplicationContext context) {
        initStrategies(context);//初始化策略
    }

    /**
     * Initialize the strategy objects that this servlet uses.
     * <p>May be overridden in subclasses in order to initialize further strategy objects.
     */
    //初始化 SpringMVC 的九大組件
    protected void initStrategies(ApplicationContext context) {
        //多文件上傳的組件
        initMultipartResolver(context);
        //初始化本地語言環境
        initLocaleResolver(context);
        //初始化模板處理器
        initThemeResolver(context);
        //handlerMapping
        initHandlerMappings(context);
        //初始化參數適配器
        initHandlerAdapters(context);
        //初始化異常攔截器
        initHandlerExceptionResolvers(context);
        //初始化視圖預處理器
        initRequestToViewNameTranslator(context);
        //初始化視圖轉換器
        initViewResolvers(context);
        //FlashMapManager
        initFlashMapManager(context);
    }

基于 Xml 的 IOC 容器的初始化

IOC 容器的初始化包括 BeanDefinition 的 Resource 定位、加載、注冊這三個基本的過程。我們以
ApplicationContext 為例講解,ApplicationContext 系列容器也許是我們最熟悉的,因為 Web 項目
中使用的 XmlWebApplicationContext 就屬于這個繼承體系,還有 ClasspathXmlApplicationContext等,其繼承體系如下圖所示


image.png

ApplicationContext 允許上下文嵌套,通過保持父上下文可以維持一個上下文體系。對于 Bean 的查找可以在這個上下文體系中發生,首先檢查當前上下文,其次是父上下文,逐級向上,這樣為不同的 Spring應用提供了一個共享的 Bean 定義環境

定位、加載和注冊的三大步驟按照下圖,總共有17個步驟,我們一步一步的看源碼


image.png
image.png

1.尋找入口

main方法啟動

ApplicationContext app = new ClassPathXmlApplicationContext("application.xml");

看其構造方法

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
    this(new String[]{configLocation}, true, (ApplicationContext)null);
}
ublic ClassPathXmlApplicationContext(
            String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
            throws BeansException {
        //調用父類的構造方法super(parent)方法為容器設置好 Bean 資源加載器
        super(parent);
        //調用父類的setConfigLocations方法設置 Bean 配置信息的定位路徑
        setConfigLocations(configLocations);
        if (refresh) {
            //AnnotationConfigApplicationContext 、 FileSystemXmlApplicationContext 、XmlWebApplicationContext 等都繼承自父容器 AbstractApplicationContext主要用到了裝飾器模和策略模式,最終都是調用 refresh()方法
            refresh();
        }
    }

AnnotationConfigApplicationContext 、 FileSystemXmlApplicationContext 、XmlWebApplicationContext 等都繼承自父容器 AbstractApplicationContext主要用到了裝飾器模和策略模式,最終都是調用 refresh()方法

2.獲得配置路徑

1、先看看ClassPathXmlApplicationContext父類的父類的父類的父類
AbstractApplicationContext 類中初始化 IOC 容器所做的主要源碼如下

public abstract class AbstractApplicationContext extends DefaultResourceLoader
        implements ConfigurableApplicationContext {
    //靜態初始化塊,在整個容器創建過程中只執行一次
    static {
//為了避免應用程序在 Weblogic8.1 關閉時出現類加載異常加載問題,加載 IOC 容
//器關閉事件(ContextClosedEvent)類
        ContextClosedEvent.class.getName();
    }

    public AbstractApplicationContext() {
        this.resourcePatternResolver = getResourcePatternResolver();
    }

    public AbstractApplicationContext(@Nullable ApplicationContext parent) {
        this();
        setParent(parent);
    }

    //獲取一個 Spring Source 的加載器用于讀入 Spring Bean 配置信息
    protected ResourcePatternResolver getResourcePatternResolver() {
//AbstractApplicationContext 繼承 DefaultResourceLoader,因此也是一個資源加載器
//Spring 資源加載器,其 getResource(String location)方法用于載入資源
        return new PathMatchingResourcePatternResolver(this);
    }
...
}

AbstractApplicationContext 的默認構造方法中有調用 PathMatchingResourcePatternResolver 的
構造方法創建 Spring 資源加載器

public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
    Assert.notNull(resourceLoader, "ResourceLoader must not be null");
    //設置 Spring 的資源加載器
    this.resourceLoader = resourceLoader;
}

在設置容器的資源加載器之后,接下來 ClassPathXmlApplicationContext 執行 setConfigLocations()方法通過調用其父類AbstractRefreshableConfigApplicationContext的方法進行對Bean配置信息的定位,該方法的源碼如下:

public abstract class AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext
        implements BeanNameAware, InitializingBean {

    @Nullable
    private String[] configLocations;
    
    String CONFIG_LOCATION_DELIMITERS = ",; \t\n";
    
    //處理單個資源文件路徑為一個字符串的情況
    public void setConfigLocation(String location) {
        //String CONFIG_LOCATION_DELIMITERS = ",; /t/n";
        //即多個資源文件路徑之間用” ,; \t\n”分隔,解析成數組形式
        setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));
    }
    
    //解析Bean定義資源文件的路徑,處理多個資源文件字符串數組
    public void setConfigLocations(@Nullable 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 配置信息,
也可以使用字符串數組,即下面兩種方式都是可以的:

ClassPathResource res = new ClassPathResource("a.xml,b.xml");
多個資源文件路徑之間可以是用” , ; \t\n”等分隔。
ClassPathResource res =new ClassPathResource(new String[]{"a.xml","b.xml"});

至此,SpringIOC 容器在初始化時將配置的 Bean 配置信息定位為 Spring 封裝的 Resource

3.容器啟動

SpringIOC 容器對 Bean 配置資源的載入是從 refresh()函數開始的,refresh()是一個模板方法,規定了IOC 容 器 的 啟 動 流 程 , 有 些 邏 輯 要 交 給 其 子 類 去 實 現 。 它 對 Bean 配 置 資 源 進 行 載 入ClassPathXmlApplicationContext 通過調用其父類 AbstractApplicationContext 的 refresh()函數啟動整個 IOC 容器對 Bean 定義的載入過程,現在我們來詳細看看 refresh()中的邏輯處理

@Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            //1.調用容器準備刷新的方法,獲取容器的當時時間,同時給容器設置同步標識
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            //2.告訴子類啟動refreshBeanFactory()方法,Bean定義資源文件的載入從
            //子類的refreshBeanFactory()方法啟動
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            //3.為BeanFactory配置容器特性,例如類加載器、事件處理器等
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                //4.為容器的某些子類指定特殊的BeanPost事件處理器
                postProcessBeanFactory(beanFactory);

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

                // Register bean processors that intercept bean creation.
                //6.為BeanFactory注冊BeanPost事件處理器.
                //BeanPostProcessor是Bean后置處理器,用于監聽容器觸發的事件
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                //7.初始化信息源,和國際化相關.
                initMessageSource();

                // Initialize event multicaster for this context.
                //8.初始化容器事件傳播器.
                initApplicationEventMulticaster();

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

                // Check for listener beans and register them.
                //10.為事件傳播器注冊事件監聽器.
                registerListeners();

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

                // Last step: publish corresponding event.
                //12.初始化容器的生命周期事件處理器,并發布容器的生命周期事件
                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.
                //13.銷毀已創建的Bean
                destroyBeans();

                // Reset 'active' flag.
                //14.取消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...
                //15.重設公共緩存
                resetCommonCaches();
            }
        }
    }

refresh()方法主要為 IOC 容器 Bean 的生命周期管理提供條件,Spring IOC 容器載入 Bean 配置信息從 其 子 類 容 器 的 refreshBeanFactory() 方 法 啟 動 , 所 以 整 個 refresh() 中
“ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();”這句以后的代碼
都是注冊容器的信息源和生命周期事件,前面說的載入就是從這句代碼開始啟動

refresh()方法的主要作用是:在創建 IOC 容器前,如果已經有容器存在,則需要把已有的容器銷毀和關閉,以保證在 refresh 之后使用的是新建立起來的 IOC 容器。它類似于對 IOC 容器的重啟,在新建立好的容器中對容器進行初始化,對 Bean 配置資源進行載入

4.創建容器

obtainFreshBeanFactory()方法調用子類容器的 refreshBeanFactory()方法,啟動容器載入 Bean 配置信息的過程,代碼如下:

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        //這里使用了委派設計模式,父類定義了抽象的refreshBeanFactory()方法,具體實現調用子類AbstractRefreshableApplicationContext容器的refreshBeanFactory()方法
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {
            logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
        }
        return beanFactory;
    }

//子類AbstractRefreshableApplicationContext的refreshBeanFactory方法
    @Override
    protected final void refreshBeanFactory() throws BeansException {
        //如果已經有容器,銷毀容器中的bean,關閉容器
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }
        try {
            //創建IOC容器
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            beanFactory.setSerializationId(getId());
            //對IOC容器進行定制化,如設置啟動參數,開啟注解的自動裝配等
            customizeBeanFactory(beanFactory);
            //調用載入Bean定義的方法,主要這里又使用了一個委派模式,在當前類中只定義了抽象的loadBeanDefinitions方法,具體的實現調用子類容器
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }

在這個refreshBeanFactory方法中,先判斷 BeanFactory 是否存在,如果存在則先銷毀 beans 并關閉 beanFactory,接著創建 DefaultListableBeanFactory,并調用loadBeanDefinitions(beanFactory)裝載 bean 定義

5.載入配置路徑

AbstractRefreshableApplicationContext 中只定義了抽象的 loadBeanDefinitions 方法,容器真正調
用的是其子類 AbstractXmlApplicationContext 對該方法的實現,AbstractXmlApplicationContext
的主要源碼如下:

public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {
    //實現父類抽象的載入Bean定義方法
    @Override
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
        // Create a new XmlBeanDefinitionReader for the given BeanFactory.
        //創建XmlBeanDefinitionReader,即創建Bean讀取器,并通過回調設置到容器中去,容  器使用該讀取器讀取Bean定義資源
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

        // Configure the bean definition reader with this context's
        // resource loading environment.
        //為Bean讀取器設置Spring資源加載器,AbstractXmlApplicationContext的
        //祖先父類AbstractApplicationContext繼承DefaultResourceLoader,因此,容器本身也是一個資源加載器
        beanDefinitionReader.setEnvironment(this.getEnvironment());
        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);
    }

    protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
        reader.setValidating(this.validating);
    }

        //Xml Bean讀取器加載Bean定義資源
    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
        //獲取Bean定義資源的定位
        Resource[] configResources = getConfigResources();
        if (configResources != null) {
            //Xml Bean讀取器調用其父類AbstractBeanDefinitionReader讀取定位
            //的Bean定義資源
            reader.loadBeanDefinitions(configResources);
        }
        //如果子類中獲取的Bean定義資源定位為空,則獲取FileSystemXmlApplicationContext構造方法中setConfigLocations方法設置的資源
        String[] configLocations = getConfigLocations();
        if (configLocations != null) {
            //Xml Bean讀取器調用其父類AbstractBeanDefinitionReader讀取定位
            //的Bean定義資源
            reader.loadBeanDefinitions(configLocations);
        }
    }

        //這里又使用了一個委托模式,調用子類的獲取Bean定義資源定位的方法
    //該方法在ClassPathXmlApplicationContext中進行實現,對于我們
    //舉例分析源碼的FileSystemXmlApplicationContext沒有使用該方法
    @Nullable
    protected Resource[] getConfigResources() {
        return null;
    }

}

以 XmlBean 讀取器的其中一種策略 XmlBeanDefinitionReader 為例。XmlBeanDefinitionReader 調用其父類AbstractBeanDefinitionReader的 reader.loadBeanDefinitions()方法讀取Bean配置資源。由于我們使用 ClassPathXmlApplicationContext 作為例子分析,因此 getConfigResources 的返回值為 null,因此程序執行 reader.loadBeanDefinitions(configLocations)分支

6.分配路徑處理策略

在 AbstractBeanDefinitionReader 的抽象父類 AbstractBeanDefinitionReader 中定義了載入過程。
AbstractBeanDefinitionReader 的 loadBeanDefinitions()方法源碼如下:

public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
        //獲取在IoC容器初始化過程中設置的資源加載器
        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 {
                //1.將指定位置的Bean定義資源文件解析為Spring IOC容器封裝的資源
                //加載多個指定位置的Bean定義資源文件
                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.
            //2.將指定位置的Bean定義資源文件解析為Spring IOC容器封裝的資源
            //加載單個指定位置的Bean定義資源文件
            Resource resource = resourceLoader.getResource(location);
            //委派調用其子類XmlBeanDefinitionReader的方法,實現加載功能
            int loadCount = loadBeanDefinitions(resource);
            if (actualResources != null) {
                actualResources.add(resource);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
            }
            return loadCount;
        }
    }

    //重載方法,調用loadBeanDefinitions(String);
    @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;
    }

從上面代碼可知該方法就做了兩件事:

  • 首先,調用資源加載器的獲取資源方法 resourceLoader.getResource(location),獲取到要加載的資源。
  • 其次,真正執行加載功能是其子類 XmlBeanDefinitionReader 的 loadBeanDefinitions()方法。在
    loadBeanDefinitions()方法中調用了 AbstractApplicationContext 的 getResources()方法,跟進去之后發現 getResources()方法其實定義在 ResourcePatternResolver 中,此時,我們有必要來看一下ResourcePatternResolver 的全類圖


    image.png

    image.png

    從上面可以看到 ResourceLoader 與 ApplicationContext 的繼承關系,可以看出其實際調用的是
    DefaultResourceLoader 中 的 getSource() 方 法 定 位 Resource , 因 為
    ClassPathXmlApplicationContext 本身就是 DefaultResourceLoader 的實現類,所以此時又回到了
    ClassPathXmlApplicationContext 中來

7.解析配置文件路徑

XmlBeanDefinitionReader 通 過 調 用 ClassPathXmlApplicationContext 的 父 類
DefaultResourceLoader 的 getResource()方法獲取要加載的資源,其源碼如下

//獲取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;
            }
        }
        //如果是類路徑的方式,那需要使用ClassPathResource 來得到bean 文件的資源對象
        if (location.startsWith("/")) {
            return getResourceByPath(location);
        }
        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 (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
            }
            catch (MalformedURLException ex) {
                // No URL -> resolve as resource path.
                //如果既不是classpath標識,又不是URL標識的Resource定位,則調用
                //容器本身的getResourceByPath方法獲取Resource
                return getResourceByPath(location);
            }
        }
    }

DefaultResourceLoader 提供了 getResourceByPath()方法的實現,就是為了處理既不是 classpath標識,又不是 URL 標識的 Resource 定位這種情況

protected Resource getResourceByPath(String path) {
    return new ClassPathContextResource(path, getClassLoader());
}

在 ClassPathResource 中完成了對整個路徑的解析。這樣,就可以從類路徑上對 IOC 配置文件進行加載,當然我們可以按照這個邏輯從任何地方加載,在 Spring 中我們看到它提供的各種資源抽象,比如ClassPathResource、URLResource、FileSystemResource 等來供我們使用。上面我們看到的是定位Resource 的一個過程,而這只是加載過程的一部分。例如 FileSystemXmlApplication 容器就重寫了getResourceByPath()方法

@Override
protected Resource getResourceByPath(String path) {
    if (path.startsWith("/")) {
      path = path.substring(1);
    }
    //這里使用文件系統資源對象來定義 bean 文件
    return new FileSystemResource(path);
}

通過子類的覆蓋,巧妙地完成了將類路徑變為文件路徑的轉換

8.開始讀取配置內容

繼續回到 XmlBeanDefinitionReader 的 loadBeanDefinitions(Resource …)方法看到代表 bean 文件的資源定義以后的載入過程。
//這里是載入XML形式Bean定義資源文件方法

    public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
        ...
                //這里是具體的讀取過程
        return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
            
        ...
    }

    //從特定XML文件中實際載入Bean定義資源的方法
    protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
            throws BeanDefinitionStoreException {
        try {
            //將XML文件轉換為DOM對象,解析過程由doLoadDocument()實現
            Document doc = doLoadDocument(inputSource, resource);
            //這里是啟動對Bean定義解析的詳細過程,該解析過程會用到Spring的Bean配置規則
            return registerBeanDefinitions(doc, resource);
        }
        ...
  }

通過源碼分析,載入 Bean 配置信息的最后一步是將 Bean 配置信息轉換為 Document 對象,該過程由doLoadDocument()方法實現

9.準備文檔對象

跟進 doLoadDocument方法

protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
        return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
                getValidationModeForResource(resource), isNamespaceAware());
    }

進入DefaultDocumentLoader類的loadDocument方法

public class DefaultDocumentLoader implements DocumentLoader {

    //使用標準的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);
        //解析Spring的Bean定義資源
        return builder.parse(inputSource);
    }

    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 對象并將其注冊到容器中的
下一章會講解解析和注冊都剩余流程:
10.分配解析策略
11.將配置載入內存
12.載入<bean>元素
13.載入<property>元素
14.載入<property>的子元素
15.載入<list>的子元素
16.分配注冊策略
17.向容器注冊

——學自咕泡學院

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容