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

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

淺談Spring與SpringMVC父子容器的關(guān)系與初始化

瀏覽:49日期:2023-08-18 13:49:37

Spring和SpringMVC的容器具有父子關(guān)系,Spring容器為父容器,SpringMVC為子容器,子容器可以引用父容器中的Bean,而父容器不可以引用子容器中的Bean。

了解了Spring與SpringMVC父子容器的關(guān)系,接下來(lái)讓我們看看Spring與SpringMVC容器的初始化過(guò)程。

以下講解使用的web.xml文件如下:

<context-param> <param-name>contextConfigLocation</param-name>//指定spring ioc配置文件的位置 <param-value>classpath*:spring/*.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener><!-- 配置DisaptcherServlet --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化參數(shù),配置springmvc配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>springMVC配置文件的路徑</param-value> </init-param> <!-- web容器啟動(dòng)時(shí)加載該Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

spring ioc容器初始化的過(guò)程

1、web應(yīng)用程序啟動(dòng)時(shí),tomcat會(huì)讀取web.xml文件中的context-parm(含有配置文件的路徑)和listener節(jié)點(diǎn),接著會(huì)為應(yīng)用程序創(chuàng)建一個(gè)ServletContext,為全局共享,Spring ioc容器就是存儲(chǔ)在這里

2、tomcat將context-param節(jié)點(diǎn)轉(zhuǎn)換為鍵值對(duì),寫入到ServletContext中

3、創(chuàng)建listener節(jié)點(diǎn)中的ContextLoaderListener實(shí)例,調(diào)用該實(shí)例,初始化webapplicationContext,這是一個(gè)接口,其實(shí)現(xiàn)類為XmlWebApplicationContext(即spring的IOC容器),其通過(guò)ServletContext.getinitialParameter('contextConfigLoaction')從ServletContext中獲取context-param中的值(即spring ioc容器配置文件的路徑),這就是為什么要有第二步的原因。接著根據(jù)配置文件的路徑加載配置文件信息(其中含有Bean的配置信息)到WebApplicationContext(即spring ioc容器)中,將WebApplicationContext以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE為屬性Key,將其存儲(chǔ)到ServletContext中,便于獲取。至此,spring ioc容器初始化完畢

4、容器初始化web.xml中配置的servlet,為其初始化自己的上下文信息servletContext,并加載其設(shè)置的配置信息到該上下文中。將WebApplicationContext(即spring ioc容器)設(shè)置為它的父容器。其中便有SpringMVC(假設(shè)配置了SpringMVC),這就是為什么spring ioc是springmvc ioc的父容器的原因

SpringMVC初始化過(guò)程

SpringMVC通過(guò)web.xml文件中servlet標(biāo)簽下的DispatcherServlet類完成自身的初始化

DispatcherServlet類的繼承體系如下:

淺談Spring與SpringMVC父子容器的關(guān)系與初始化

請(qǐng)注意每個(gè)長(zhǎng)方形中第三行的方法,其為完成SpringMVC ioc容器初始化的關(guān)鍵。

我們知道,每個(gè)servlet在初始化時(shí),會(huì)先調(diào)用servlte的構(gòu)造函數(shù)(為默認(rèn)構(gòu)造函數(shù)),接著調(diào)用init函數(shù),而DispatcherServlet的init方法在其父類HttpServlet中。

HttpServlet中的init方法

/DispatcherServlet第一次加載時(shí)調(diào)用init方法@Override public final void init() throws ServletException { if (logger.isDebugEnabled()) { logger.debug('Initializing servlet ’' + getServletName() + '’'); } // Set bean properties from init parameters. try {/*加載web.xml文件中的servlet標(biāo)簽中的init-param,其中含有springMVC的配置文件的名字和路徑 *若沒(méi)有,則默認(rèn)為(servlet-name)-servlet.xml, *默認(rèn)路徑為WEF—INF下 */ PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); //創(chuàng)建BeanWrapper實(shí)例,為DispatcherServlet設(shè)置屬性 BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment())); initBeanWrapper(bw); //把init-param中的參數(shù)設(shè)置到DispatcherServlet里面去 bw.setPropertyValues(pvs, true); } catch (BeansException ex) { logger.error('Failed to set bean properties on servlet ’' + getServletName() + '’', ex); throw ex; } // Let subclasses do whatever initialization they like. //該方法在FrameworkServlet中 initServletBean(); if (logger.isDebugEnabled()) { logger.debug('Servlet ’' + getServletName() + '’ configured successfully'); } }

FrameworkServlet中的initServletBean方法

@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 { //創(chuàng)建springmvc的ioc容器實(shí)例 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'); } }

FrameworkServlet中的initWebapplicationContext方法

protected WebApplicationContext initWebApplicationContext() { //首先通過(guò)ServletContext獲得spring容器,因?yàn)樽尤萜鱯pringMVC要和父容器spring容器進(jìn)行關(guān)聯(lián) //這就是為什么要在ServletContext中注冊(cè)spring ioc容器的原因 WebApplicationContext rootContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); //定義springMVC容器wac WebApplicationContext wac = null; //判斷容器是否由編程式傳入(即是否已經(jīng)存在了容器實(shí)例),存在的話直接賦值給wac,給springMVC容器設(shè)置父容器 //最后調(diào)用刷新函數(shù)configureAndRefreshWebApplicationContext(wac),作用是把springMVC的配置信息加載到容器中去(之前已經(jīng)將配置信息的路徑設(shè)置到了bw中) 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()) { 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 //將spring ioc設(shè)置為springMVC ioc的父容器 cwac.setParent(rootContext); } configureAndRefreshWebApplicationContext(cwac);} } } if (wac == null) { // 在ServletContext中尋找是否有springMVC容器,初次運(yùn)行是沒(méi)有的,springMVC初始化完畢ServletContext就有了springMVC容器 wac = findWebApplicationContext(); } //當(dāng)wac既沒(méi)有沒(méi)被編程式注冊(cè)到容器中的,也沒(méi)在ServletContext找得到,此時(shí)就要新建一個(gè)springMVC容器 if (wac == null) { // 創(chuàng)建springMVC容器 wac = createWebApplicationContext(rootContext); } if (!this.refreshEventReceived) { //到這里mvc的容器已經(jīng)創(chuàng)建完畢,接著才是真正調(diào)用DispatcherServlet的初始化方法onRefresh(wac) onRefresh(wac); } if (this.publishContext) { //將springMVC容器存放到ServletContext中去,方便下次取出來(lái) 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; }

FrameworkServlet中的createWebApplicationContext(WebApplicationContext parent)方法

protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) { Class<?> contextClass = getContextClass(); if (this.logger.isDebugEnabled()) { this.logger.debug('Servlet with name ’' + getServletName() + '’ will try to create custom WebApplicationContext context of class ’' + contextClass.getName() + '’' + ', using parent context [' + parent + ']'); } if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException( 'Fatal initialization error in servlet with name ’' + getServletName() + '’: custom WebApplicationContext class [' + contextClass.getName() + '] is not of type ConfigurableWebApplicationContext'); } //實(shí)例化空白的ioc容器 ConfigurableWebApplicationContext wac =(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); //給容器設(shè)置環(huán)境 wac.setEnvironment(getEnvironment()); //給容器設(shè)置父容器(就是spring容器),兩個(gè)ioc容器關(guān)聯(lián)在一起了 wac.setParent(parent); //給容器加載springMVC的配置信息,之前已經(jīng)通過(guò)bw將配置文件路徑寫入到了DispatcherServlet中 wac.setConfigLocation(getContextConfigLocation()); //上面提到過(guò)這方法,刷新容器,根據(jù)springMVC配置文件完成初始化操作,此時(shí)springMVC容器創(chuàng)建完成 configureAndRefreshWebApplicationContext(wac); return wac; }

DispatcherServlet的onRefresh(ApplicationContext context)方法

@Override protected void onRefresh(ApplicationContext context) { initStrategies(context); }

DispatcherServlet的initStrategies(ApplicationContext context)方法

protected void initStrategies(ApplicationContext context) { initMultipartResolver(context);//文件上傳解析 initLocaleResolver(context);//本地解析 initThemeResolver(context);//主題解析 initHandlerMappings(context);//url請(qǐng)求映射 initHandlerAdapters(context);//初始化真正調(diào)用controloler方法的類 initHandlerExceptionResolvers(context);//異常解析 initRequestToViewNameTranslator(context); initViewResolvers(context);//視圖解析 initFlashMapManager(context); }

總結(jié)以下DispatcherServlet及各個(gè)父類(接口)的功能:

HttpServlet:實(shí)現(xiàn)了init方法,完成web,xml中與DispatcherServlet有關(guān)的參數(shù)的讀入,初始化DispatcherServlet。

FrameworkServlet:完成了springMVC ioc 容器的創(chuàng)建,并且將spring ioc容器設(shè)置為springMVC ioc容器的父容器,將springMVC ioc容器注冊(cè)到ServletContext中

DispatcherServlet:完成策略組件的初始化

至此,SpringMVC容器初始化完成

以上這篇淺談Spring與SpringMVC父子容器的關(guān)系與初始化就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | BHK汞灯-百科|上海熙浩实业有限公司| 温州中研白癜风专科_温州治疗白癜风_温州治疗白癜风医院哪家好_温州哪里治疗白癜风 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 企业彩铃制作_移动、联通、电信集团彩铃上传开通_彩铃定制_商务彩铃管理平台-集团彩铃网 | 北京乾茂兴业科技发展有限公司 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | 400电话_400电话申请_866元/年_【400电话官方业务办理】-俏号网 3dmax渲染-效果图渲染-影视动画渲染-北京快渲科技有限公司 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 学考网学历中心| 无锡网站建设_小程序制作_网站设计公司_无锡网络公司_网站制作 | 洗地机-全自动/手推式洗地机-扫地车厂家_扬子清洁设备 | 溶氧传感器-pH传感器|哈美顿(hamilton) | LHH药品稳定性试验箱-BPS系列恒温恒湿箱-意大利超低温冰箱-上海一恒科学仪器有限公司 | 北钻固控设备|石油钻采设备-石油固控设备厂家 | 医养体检包_公卫随访箱_慢病随访包_家签随访包_随访一体机-济南易享医疗科技有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 浩方智通 - 防关联浏览器 - 跨境电商浏览器 - 云雀浏览器 | 发电机价格|发电机组价格|柴油发电机价格|柴油发电机组价格网 | 无锡装修装潢公司,口碑好的装饰装修公司-无锡索美装饰设计工程有限公司 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 万濠投影仪_瑞士TRIMOS高度仪_尼康投影仪V12BDC|量子仪器 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | 企业微信scrm管理系统_客户关系管理平台_私域流量运营工具_CRM、ERP、OA软件-腾辉网络 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 紧急泄压人孔_防爆阻火器_阻火呼吸阀[河北宏泽石化] | 天品互联-北京APP开发公司-小程序开发制作-软件开发 | 塑料托盘厂家直销-吹塑托盘生产厂家-力库塑业【官网】 | 冷库安装厂家_杭州冷库_保鲜库建设-浙江克冷制冷设备有限公司 | 质检报告_CE认证_FCC认证_SRRC认证_PSE认证_第三方检测机构-深圳市环测威检测技术有限公司 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 深圳市东信高科自动化设备有限公司| 消泡剂-水处理消泡剂-涂料消泡剂-切削液消泡剂价格-东莞德丰消泡剂厂家 | 河南生物显微镜,全自动冰冻切片机-河南荣程联合科技有限公司 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 |