电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Spring 加載多個xml配置文件的原理分析

瀏覽:3日期:2023-07-07 16:09:25
目錄示例spring-configlication.xml:spring-config-instance-factory.xmljava示例代碼實現AbstractRefreshableConfigApplicationContextAbstractApplicationContextAbstractRefreshableApplicationContextAbstractXmlApplicationContext示例

先給出兩個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 /> <!--實例工廠方法創建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);} }}

這樣我們就會在控制臺打印出兩個配置文件中所有的Bean.

personChineseFemaleSingercarFactoryinstanceCarbrandProcess finished with exit code 0實現AbstractRefreshableConfigApplicationContext

從類的名字推導出這是一個帶刷新功能并且帶配置功能的應用上下文。

/** * 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;} }

這個方法很好理解,首先根據傳入配置文件路徑字符串數組遍歷,并且里面調用了resolvePath方法解析占位符。那么我們要想下了,這里只做了解析占位符并且把字符串賦值給configLocations變量,那必然肯定會在什么時候去讀取這個路徑下的文件并加載bean吧?會不會是在應用上下文調用refresh方法的時候去加載呢?帶著思考我們來到了應用上下文的refresh方法。

AbstractApplicationContext

@Override public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) { // Tell the subclass to refresh the internal bean factory. //告訴子類刷新 內部BeanFactory //https://www.iteye.com/blog/rkdu2-163-com-2003638 //內部會加載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//里面會加載bean定義//todo 加載bean定義refreshBeanFactory();//如果beanFactory為null 會報錯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 實現了此方法 //GenericApplicationContext 實現了此方法 protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

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

AbstractRefreshableApplicationContext

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

Spring 加載多個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(); //設置beanFactory = null}try { DefaultListableBeanFactory beanFactory = createBeanFactory(); //加載Bean定義 //todo AbstractXmlApplicationContext 子類實現 放入beandefinitionMap中 loadBeanDefinitions(beanFactory);}catch (IOException ex) { throw new ApplicationContextException('I/O error parsing bean definition source for ' + getDisplayName(), ex);} }

這里就看到了前篇文章提到一個很重要的方法loadBeanDefinitions,我們再拿出來回顧下加深理解:

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

這里因為我們是采用xml配置的,那么肯定是XmlBeanDefinitionReader無疑,我們再回顧下實現此方法的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 方法來處理 //這個方法只是 去加載和注冊 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);} }

這里我們終于到了這個configLocations的用武之地,它就傳入了XmlBeanDefinitionReader的loadBeanDefinitions方法中。在這里我們也看到了Spring首先會根據configResources加載BeanDefinition,其次才會去根據configLocations配置去加載BeanDefinition。到這里我們可以學到Spring中對面向對象中封裝,繼承和多態的運用。下篇文章我們繼續剖析Spring關于Xml加載Bean定義的點滴。

以上就是Spring 加載多個xml配置文件的原理分析的詳細內容,更多關于Spring 加載xml配置文件的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | LED投光灯-工矿灯-led路灯头-工业灯具 - 山东普瑞斯照明科技有限公司 | 二手Sciex液质联用仪-岛津气质联用仪-二手安捷伦气质联用仪-上海隐智科学仪器有限公司 | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 有源电力滤波装置-电力有源滤波器-低压穿排电流互感器|安科瑞 | 【连江县榕彩涂料有限公司】官方网站| 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 超声波清洗机-超声波清洗设备定制生产厂家 - 深圳市冠博科技实业有限公司 | vr安全体验馆|交通安全|工地安全|禁毒|消防|安全教育体验馆|安全体验教室-贝森德(深圳)科技 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 快速门厂家批发_PVC快速卷帘门_高速门_高速卷帘门-广州万盛门业 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 氟塑料磁力泵-不锈钢离心泵-耐腐蚀化工泵厂家「皖金泵阀」 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 防渗土工膜|污水处理防渗膜|垃圾填埋场防渗膜-泰安佳路通工程材料有限公司 | 今日热点_实时热点_奇闻异事_趣闻趣事_灵异事件 - 奇闻事件 | 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 学考网学历中心| 紫外荧光硫分析仪-硫含量分析仪-红外光度测定仪-泰州美旭仪器 | 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | 南京蜂窝纸箱_南京木托盘_南京纸托盘-南京博恒包装有限公司 | 石磨面粉机|石磨面粉机械|石磨面粉机组|石磨面粉成套设备-河南成立粮油机械有限公司 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 济南品牌设计-济南品牌策划-即合品牌策划设计-山东即合官网 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 购买舔盐、舔砖、矿物质盐压块机,鱼饵、鱼饲料压块机--请到杜甫机械 | 菲希尔X射线测厚仪-菲希尔库伦法测厚仪-无锡骏展仪器有限责任公司 | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 天津货架厂_穿梭车货架_重型仓储货架_阁楼货架定制-天津钢力仓储货架生产厂家_天津钢力智能仓储装备 | 鲁网 - 山东省重点新闻网站,山东第一财经门户| 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 贵州水玻璃_-贵阳花溪闽兴水玻璃厂 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | pH污水传感器电极,溶解氧电极传感器-上海科蓝仪表科技有限公司 | 海南在线 海南一家|