日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:31日期:2023-07-07 16:09:25
目錄示例spring-configlication.xml:spring-config-instance-factory.xmljava示例代碼實(shí)現(xiàn)AbstractRefreshableConfigApplicationContextAbstractApplicationContextAbstractRefreshableApplicationContextAbstractXmlApplicationContext示例

先給出兩個(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 class='com.john.aop.Person'> </bean> <bean abstract='true' ><property name='country' value='中國'/><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 /> <!--實(shí)例工廠方法創(chuàng)建bean--> <bean factory-bean='carFactory' factory-method='createCar'><constructor-arg ref='brand'/> </bean> <bean /></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.

personChineseFemaleSingercarFactoryinstanceCarbrandProcess 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í)候去加載呢?帶著思考我們來到了應(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() */ //得到刷新過的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)此方法來完成最終的配置加載,那實(shí)現(xiàn)此方法的Spring內(nèi)部默認(rèn)有兩個(gè)類,AbstractRefreshableApplicationContext和GenericApplicationContext,這里我們就關(guān)心AbstractRefreshableApplicationContext:

AbstractRefreshableApplicationContext

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

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

/** * 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è)倌贸鰜砘仡櫹录由罾斫猓?/p>

/** * 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無疑,我們?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 方法來處理 //這個(gè)方法只是 去加載和注冊(cè) bean定義 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {//ClassPathXmlApplicationContextResource[] 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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
美女毛片一区二区三区四区| 国产乱码精品一区二区三区四区 | 亚洲精品福利| 亚洲色图综合| 国产精品美女在线观看直播| 麻豆精品蜜桃视频网站| 日韩在线二区| 久久国产高清| 国产欧美88| 天堂日韩电影| 蜜桃av一区二区| 婷婷精品在线观看| 国产精品网址| 亚洲天堂1区| 三级欧美在线一区| 国产精品久久久久久久免费软件| 中文字幕色婷婷在线视频| 樱桃成人精品视频在线播放| 亚洲欧洲国产精品一区| 国产激情综合| 国产精品美女久久久浪潮软件| 国产亚洲精品美女久久久久久久久久| 精品国产a一区二区三区v免费| 亚洲精品a级片| 欧美三级第一页| 日产精品一区| 亚洲精品国模| 国产精品欧美一区二区三区不卡| 成人日韩在线| 日韩精品免费视频人成 | 三级亚洲高清视频| 国产精久久一区二区| 精品捆绑调教一区二区三区| 综合国产精品| 色综合五月天| 亚洲精品动态| 午夜精品成人av| 日韩一区二区三区四区五区| 久久男女视频| 日韩动漫一区| 成人片免费看| 欧美在线看片| av一区二区高清| 日韩激情精品| 1000部精品久久久久久久久| 国产亚洲电影| 水蜜桃久久夜色精品一区的特点| 久久精品免视看国产成人| 爽好久久久欧美精品| 国产一区二区亚洲| 亚洲三级国产| 免费不卡中文字幕在线| 精品五月天堂| 日韩中文字幕| 亚洲午夜黄色| 久久av国产紧身裤| 首页欧美精品中文字幕| 日产精品一区二区| 欧美天堂一区| 亚洲专区视频| 欧美91精品| 亚洲精品**中文毛片| 91成人精品在线| 国产精品三上| www.com.cn成人| 久久wwww| 日本亚洲最大的色成网站www | 1024精品久久久久久久久| 精品一区二区男人吃奶| 欧美天堂一区| 亚洲欧洲国产精品一区| 午夜一级在线看亚洲| 久久婷婷激情| 成人va天堂| 青青青免费在线视频| 国产a久久精品一区二区三区| 国产精品一区二区三区av麻| 亚洲综合激情在线| 日韩精品一二三| 国产毛片久久| 久久一区二区三区电影| 欧美日韩尤物久久| 中文字幕系列一区| 久久精品亚洲人成影院| 天堂√8在线中文| 国产色播av在线| 日韩精品2区| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 99久久久久久中文字幕一区| 欧美黄色网页| 日韩福利一区| 日韩成人a**站| 色爱综合网欧美| 色老板在线视频一区二区| 99久久九九| 亚洲作爱视频| 少妇精品久久久| 91精品国产自产观看在线| 69精品国产久热在线观看| 国产精品密蕾丝视频下载| 精品一区视频| 日韩欧美国产精品综合嫩v| 在线精品亚洲欧美日韩国产| 成人精品中文字幕| 精品1区2区3区4区| 亚洲一区欧美激情| 日韩一区二区三区高清在线观看| 国产午夜精品一区在线观看| 欧美精品二区| 免费在线小视频| 午夜久久影院| 日韩精品午夜视频| 国产极品久久久久久久久波多结野| 国产精品theporn| 日本不卡免费高清视频在线| 欧美搞黄网站| 日韩激情中文字幕| 精品视频高潮| 久久精品国产大片免费观看| 久久亚洲风情| 91精品国产自产在线丝袜啪| 精品久久网站| 亚洲精品中文字幕乱码| 在线观看一区| 精品免费视频| 亚洲美洲欧洲综合国产一区| 日本综合精品一区| 国产成人免费av一区二区午夜| 久久中文字幕av| 日韩国产一二三区| 色在线中文字幕| 亚洲资源在线| 色欧美自拍视频| 男女性色大片免费观看一区二区| 国产精品草草| 亚洲成人精品| 日本三级亚洲精品| 日韩免费小视频| 日本免费新一区视频| 日韩不卡在线| 青青草91视频| 图片区亚洲欧美小说区| 国产精品一区二区三区av| japanese国产精品| 久久av中文| 日本欧美在线看| 日韩成人综合| 欧美日韩一区自拍| 在线一区电影| 欧美国产偷国产精品三区| 深夜福利亚洲| 欧美成人亚洲| 国产成人精品福利| 亚洲精品乱码| 亚洲韩日在线| 美女国产一区二区三区| 免播放器亚洲| 99精品电影| 国产一区二区三区视频在线| 日韩欧美在线精品| 欧美日韩高清| 日韩欧美二区| 国产精品草草| 蜜臀av一区二区在线免费观看 | 国产精品伦一区二区| 国产农村妇女精品一二区| 欧美国产一级| 国产欧美自拍| 亚洲精品福利| 日韩亚洲在线| 日韩欧美综合| 国产精品va| 日韩三级一区| 老牛国产精品一区的观看方式| 免费看av不卡| 国产激情久久| 久久国产精品免费一区二区三区 | 国产精品成人3p一区二区三区| 美女国产精品| 久久视频精品| 欧美gv在线| 国产一区二区三区成人欧美日韩在线观看| 中文字幕亚洲精品乱码| 国产高清久久| 999国产精品| 日韩高清欧美| 激情综合五月| 精品视频一区二区三区在线观看 | 日韩久久一区二区三区| 国产一区三区在线播放| 国产精品色在线网站| 日本va欧美va精品发布| 美国三级日本三级久久99| 久久午夜影视| 午夜在线精品偷拍| 黄色成人在线网址| 亚洲黄色影院| 亚洲精品电影| 亚洲欧美日韩国产一区二区| 美女黄网久久|