最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring 加載多個(gè)xml配置文件的原理分析

 更新時(shí)間:2021年06月22日 14:20:31   作者:沉迷Spring  
我們知道Spring一次可以加載多個(gè)Bean定義的Xml配置文件,我們可以設(shè)想下如果讓我們來(lái)做我們會(huì)怎么做?我估計(jì)會(huì)根據(jù)配置文件的順序依次讀取并加載,那再來(lái)看看Spring是如何做的?

示例

先給出兩個(gè)Bean的配置文件:

spring-configlication.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="person" class="com.john.aop.Person">
    </bean>
    <bean id="ChineseFemaleSinger" class="com.john.beanFactory.Singer" abstract="true" >
        <property name="country" value="中國(guó)"/>
        <property name="gender" value="女"/>
    </bean>

</beans>

spring-config-instance-factory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="carFactory" class="com.john.domain.CarFactory" />
    <!--實(shí)例工廠方法創(chuàng)建bean-->
    <bean id="instanceCar" factory-bean="carFactory" factory-method="createCar">
        <constructor-arg ref="brand"/>
    </bean>
    <bean id="brand" class="com.john.domain.Brand" />
</beans>

java示例代碼

public class ConfigLocationsDemo {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();
        applicationContext.setConfigLocations("spring-configlocation.xml","spring-config-instance-factory.xml");
        applicationContext.refresh();
         String[] beanNames = applicationContext.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }
}

這樣我們就會(huì)在控制臺(tái)打印出兩個(gè)配置文件中所有的Bean.

person
ChineseFemaleSinger
carFactory
instanceCar
brand

Process finished with exit code 0

實(shí)現(xiàn)

AbstractRefreshableConfigApplicationContext

從類的名字推導(dǎo)出這是一個(gè)帶刷新功能并且?guī)渲霉δ艿膽?yīng)用上下文。

/**
     * Set the config locations for this application context.
     * <p>If not set, the implementation may use a default as appropriate.
     */
    public void setConfigLocations(@Nullable String... locations) {
        if (locations != null) {

            this.configLocations = new String[locations.length];
            for (int i = 0; i < locations.length; i++) {
                this.configLocations[i] = resolvePath(locations[i]).trim();
            }
        }
        else {
            this.configLocations = null;
        }
    }

這個(gè)方法很好理解,首先根據(jù)傳入配置文件路徑字符串?dāng)?shù)組遍歷,并且里面調(diào)用了resolvePath方法解析占位符。那么我們要想下了,這里只做了解析占位符并且把字符串賦值給configLocations變量,那必然肯定會(huì)在什么時(shí)候去讀取這個(gè)路徑下的文件并加載bean吧?會(huì)不會(huì)是在應(yīng)用上下文調(diào)用refresh方法的時(shí)候去加載呢?帶著思考我們來(lái)到了應(yīng)用上下文的refresh方法。

AbstractApplicationContext

     @Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {

            // Tell the subclass to refresh the internal bean factory.
            //告訴子類刷新 內(nèi)部BeanFactory
            //https://www.iteye.com/blog/rkdu2-163-com-2003638
            //內(nèi)部會(huì)加載bean定義
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
      }
    }

    /**
     * Tell the subclass to refresh the internal bean factory.
     * @return the fresh BeanFactory instance
     * @see #refreshBeanFactory()
     * @see #getBeanFactory()
     */
    //得到刷新過(guò)的beanFactory
    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        //抽象類AbstractRefreshableApplicationContext
        //里面會(huì)加載bean定義
        //todo 加載bean定義
        refreshBeanFactory();
        //如果beanFactory為null 會(huì)報(bào)錯(cuò)
        return getBeanFactory();
    }

    /**
     * Subclasses must implement this method to perform the actual configuration load.
     * The method is invoked by {@link #refresh()} before any other initialization work.
     * <p>A subclass will either create a new bean factory and hold a reference to it,
     * or return a single BeanFactory instance that it holds. In the latter case, it will
     * usually throw an IllegalStateException if refreshing the context more than once.
     * @throws BeansException if initialization of the bean factory failed
     * @throws IllegalStateException if already initialized and multiple refresh
     * attempts are not supported
     */
    //AbstractRefreshableApplicationContext 實(shí)現(xiàn)了此方法
    //GenericApplicationContext 實(shí)現(xiàn)了此方法
    protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

我們看到refreshBeanFactory的第一句注釋就提到了Subclasses must implement this method to perform the actual configuration load,意思就是子類必須實(shí)現(xiàn)此方法來(lái)完成最終的配置加載,那實(shí)現(xiàn)此方法的Spring內(nèi)部默認(rèn)有兩個(gè)類,AbstractRefreshableApplicationContext和GenericApplicationContext,這里我們就關(guān)心AbstractRefreshableApplicationContext:

AbstractRefreshableApplicationContext

我們要時(shí)刻記得上面的AbstractRefreshableConfigApplicationContext類是繼承于AbstractRefreshableApplicationContext的,到這里我們給張類關(guān)系圖以便加深理解:

/**
     * This implementation performs an actual refresh of this context's underlying
     * bean factory, shutting down the previous bean factory (if any) and
     * initializing a fresh bean factory for the next phase of the context's lifecycle.
     */
    @Override
    protected final void refreshBeanFactory() throws BeansException {

         //判斷beanFactory 是否為空
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory(); //設(shè)置beanFactory = null
        }
        try {
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            //加載Bean定義
            //todo AbstractXmlApplicationContext 子類實(shí)現(xiàn) 放入beandefinitionMap中
            loadBeanDefinitions(beanFactory);
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }

這里就看到了前篇文章提到一個(gè)很重要的方法loadBeanDefinitions,我們?cè)倌贸鰜?lái)回顧下加深理解:

/**
     * Load bean definitions into the given bean factory, typically through
     * delegating to one or more bean definition readers.
     * @param beanFactory the bean factory to load bean definitions into
     * @throws BeansException if parsing of the bean definitions failed
     * @throws IOException if loading of bean definition files failed
     * @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
     * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
     */
    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
            throws BeansException, IOException;

這里因?yàn)槲覀兪遣捎脁ml配置的,那么肯定是XmlBeanDefinitionReader無(wú)疑,我們?cè)倩仡櫹聦?shí)現(xiàn)此方法的AbstractXmlApplicationContext:

AbstractXmlApplicationContext

/**
     * Loads the bean definitions via an XmlBeanDefinitionReader.
     * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
     * @see #initBeanDefinitionReader
     * @see #loadBeanDefinitions
     */
    //todo 重載了 AbstractRefreshableApplicationContext
    @Override
    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
        //忽略代碼。。
        loadBeanDefinitions(beanDefinitionReader);
    }

    /**
     * Load the bean definitions with the given XmlBeanDefinitionReader.
     * <p>The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}
     * method; hence this method is just supposed to load and/or register bean definitions.
     * @param reader the XmlBeanDefinitionReader to use
     * @throws BeansException in case of bean registration errors
     * @throws IOException if the required XML document isn't found
     * @see #refreshBeanFactory
     * @see #getConfigLocations
     * @see #getResources
     * @see #getResourcePatternResolver
     */
    //bean工廠的生命周期由 refreshBeanFactory 方法來(lái)處理
    //這個(gè)方法只是 去加載和注冊(cè) bean定義
    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
        //ClassPathXmlApplicationContext
        Resource[] configResources = getConfigResources();
        if (configResources != null) {
            reader.loadBeanDefinitions(configResources);
        }
        String[] configLocations = getConfigLocations();
        if (configLocations != null) {
            //抽象AbstractBeanDefinitionReader里去加載
            reader.loadBeanDefinitions(configLocations);
        }
    }

這里我們終于到了這個(gè)configLocations的用武之地,它就傳入了XmlBeanDefinitionReader的loadBeanDefinitions方法中。在這里我們也看到了Spring首先會(huì)根據(jù)configResources加載BeanDefinition,其次才會(huì)去根據(jù)configLocations配置去加載BeanDefinition。到這里我們可以學(xué)到Spring中對(duì)面向?qū)ο笾蟹庋b,繼承和多態(tài)的運(yùn)用。下篇文章我們繼續(xù)剖析Spring關(guān)于Xml加載Bean定義的點(diǎn)滴。

以上就是Spring 加載多個(gè)xml配置文件的原理分析的詳細(xì)內(nèi)容,更多關(guān)于Spring 加載xml配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Spring Boot使用系統(tǒng)參數(shù)表提升系統(tǒng)的靈活性

    詳解Spring Boot使用系統(tǒng)參數(shù)表提升系統(tǒng)的靈活性

    Spring Boot項(xiàng)目中常有一些相對(duì)穩(wěn)定的參數(shù)設(shè)置項(xiàng),其作用范圍是系統(tǒng)級(jí)的或模塊級(jí)的,這些參數(shù)稱為系統(tǒng)參數(shù)。這些變量以參數(shù)形式進(jìn)行配置,從而提高變動(dòng)和擴(kuò)展的靈活性,保持代碼的穩(wěn)定性
    2021-06-06
  • java容器類知識(shí)點(diǎn)詳細(xì)總結(jié)

    java容器類知識(shí)點(diǎn)詳細(xì)總結(jié)

    這篇文章主要介紹了java容器類知識(shí)點(diǎn)詳細(xì)總結(jié),
    2019-06-06
  • HashMap原理的深入理解

    HashMap原理的深入理解

    這篇文章主要介紹了對(duì)HashMap原理的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 用Java將字符串的首字母轉(zhuǎn)換大小寫(xiě)

    用Java將字符串的首字母轉(zhuǎn)換大小寫(xiě)

    在項(xiàng)目開(kāi)發(fā)的時(shí)候會(huì)需要統(tǒng)一字符串的格式,比如首字母要求統(tǒng)一大寫(xiě)或小寫(xiě),那用Java如何實(shí)現(xiàn)這一功能?下面一起來(lái)學(xué)習(xí)學(xué)習(xí)。
    2016-08-08
  • Spring?@Bean?修飾方法時(shí)注入?yún)?shù)的操作方法

    Spring?@Bean?修飾方法時(shí)注入?yún)?shù)的操作方法

    對(duì)于 Spring 而言,IOC 容器中的 Bean 對(duì)象的創(chuàng)建和使用是一大重點(diǎn),Spring 也為我們提供了注解方式創(chuàng)建 bean 對(duì)象:使用 @Bean,這篇文章主要介紹了Spring?@Bean?修飾方法時(shí)如何注入?yún)?shù),需要的朋友可以參考下
    2023-10-10
  • MybatisPlus分頁(yè)失效不起作用的解決

    MybatisPlus分頁(yè)失效不起作用的解決

    在使用MybatisPlus的selectPage時(shí)發(fā)現(xiàn)分頁(yè)不起作用,每次返回的都是全部的數(shù)據(jù),本文就來(lái)介紹一下MybatisPlus分頁(yè)失效不起作用的解決,感興趣的可以了解一下
    2024-03-03
  • mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor示例

    mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor示例

    這篇文章主要介紹了mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 基于Feign實(shí)現(xiàn)異步調(diào)用

    基于Feign實(shí)現(xiàn)異步調(diào)用

    近期,需要對(duì)之前的接口進(jìn)行優(yōu)化,縮短接口的響應(yīng)時(shí)間,但是springcloud中的feign是不支持傳遞異步化的回調(diào)結(jié)果的,因此有了以下的解決方案,記錄一下,需要的朋友可以參考下
    2021-05-05
  • Java中Word與PDF轉(zhuǎn)換為圖片的方法詳解

    Java中Word與PDF轉(zhuǎn)換為圖片的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)將Word與PDF轉(zhuǎn)換為圖片,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-10-10
  • 實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問(wèn)題的解決辦法

    實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問(wèn)題的解決辦法

    今天小編就為大家分享一篇關(guān)于實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問(wèn)題的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02

最新評(píng)論

桑日县| 芮城县| 巴里| 绵阳市| 许昌市| 封丘县| 贵德县| 汽车| 同江市| 五原县| 元氏县| 瓦房店市| 望江县| 灵寿县| 千阳县| 祁阳县| 大安市| 鹿泉市| 罗江县| 福州市| 东乌珠穆沁旗| 吴桥县| 阿坝县| 新巴尔虎右旗| 蛟河市| 吉木乃县| 广东省| 广灵县| 台东市| 加查县| 宣恩县| 沂南县| 腾冲县| 任丘市| 连城县| 黄陵县| 临江市| 永川市| 宽城| 新民市| 内黄县|