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

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

淺談Spring與SpringMVC父子容器的關系與初始化

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

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

了解了Spring與SpringMVC父子容器的關系,接下來讓我們看看Spring與SpringMVC容器的初始化過程。

以下講解使用的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> <!-- 初始化參數,配置springmvc配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>springMVC配置文件的路徑</param-value> </init-param> <!-- web容器啟動時加載該Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

spring ioc容器初始化的過程

1、web應用程序啟動時,tomcat會讀取web.xml文件中的context-parm(含有配置文件的路徑)和listener節點,接著會為應用程序創建一個ServletContext,為全局共享,Spring ioc容器就是存儲在這里

2、tomcat將context-param節點轉換為鍵值對,寫入到ServletContext中

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

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

SpringMVC初始化過程

SpringMVC通過web.xml文件中servlet標簽下的DispatcherServlet類完成自身的初始化

DispatcherServlet類的繼承體系如下:

淺談Spring與SpringMVC父子容器的關系與初始化

請注意每個長方形中第三行的方法,其為完成SpringMVC ioc容器初始化的關鍵。

我們知道,每個servlet在初始化時,會先調用servlte的構造函數(為默認構造函數),接著調用init函數,而DispatcherServlet的init方法在其父類HttpServlet中。

HttpServlet中的init方法

/DispatcherServlet第一次加載時調用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標簽中的init-param,其中含有springMVC的配置文件的名字和路徑 *若沒有,則默認為(servlet-name)-servlet.xml, *默認路徑為WEF—INF下 */ PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); //創建BeanWrapper實例,為DispatcherServlet設置屬性 BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment())); initBeanWrapper(bw); //把init-param中的參數設置到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 { //創建springmvc的ioc容器實例 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() { //首先通過ServletContext獲得spring容器,因為子容器springMVC要和父容器spring容器進行關聯 //這就是為什么要在ServletContext中注冊spring ioc容器的原因 WebApplicationContext rootContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); //定義springMVC容器wac WebApplicationContext wac = null; //判斷容器是否由編程式傳入(即是否已經存在了容器實例),存在的話直接賦值給wac,給springMVC容器設置父容器 //最后調用刷新函數configureAndRefreshWebApplicationContext(wac),作用是把springMVC的配置信息加載到容器中去(之前已經將配置信息的路徑設置到了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設置為springMVC ioc的父容器 cwac.setParent(rootContext); } configureAndRefreshWebApplicationContext(cwac);} } } if (wac == null) { // 在ServletContext中尋找是否有springMVC容器,初次運行是沒有的,springMVC初始化完畢ServletContext就有了springMVC容器 wac = findWebApplicationContext(); } //當wac既沒有沒被編程式注冊到容器中的,也沒在ServletContext找得到,此時就要新建一個springMVC容器 if (wac == null) { // 創建springMVC容器 wac = createWebApplicationContext(rootContext); } if (!this.refreshEventReceived) { //到這里mvc的容器已經創建完畢,接著才是真正調用DispatcherServlet的初始化方法onRefresh(wac) onRefresh(wac); } if (this.publishContext) { //將springMVC容器存放到ServletContext中去,方便下次取出來 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'); } //實例化空白的ioc容器 ConfigurableWebApplicationContext wac =(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); //給容器設置環境 wac.setEnvironment(getEnvironment()); //給容器設置父容器(就是spring容器),兩個ioc容器關聯在一起了 wac.setParent(parent); //給容器加載springMVC的配置信息,之前已經通過bw將配置文件路徑寫入到了DispatcherServlet中 wac.setConfigLocation(getContextConfigLocation()); //上面提到過這方法,刷新容器,根據springMVC配置文件完成初始化操作,此時springMVC容器創建完成 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請求映射 initHandlerAdapters(context);//初始化真正調用controloler方法的類 initHandlerExceptionResolvers(context);//異常解析 initRequestToViewNameTranslator(context); initViewResolvers(context);//視圖解析 initFlashMapManager(context); }

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

HttpServlet:實現了init方法,完成web,xml中與DispatcherServlet有關的參數的讀入,初始化DispatcherServlet。

FrameworkServlet:完成了springMVC ioc 容器的創建,并且將spring ioc容器設置為springMVC ioc容器的父容器,將springMVC ioc容器注冊到ServletContext中

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

至此,SpringMVC容器初始化完成

以上這篇淺談Spring與SpringMVC父子容器的關系與初始化就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
美女网站一区| 欧美精选一区二区三区| 激情综合自拍| 成人在线网站| 欧洲亚洲一区二区三区| 麻豆视频在线观看免费网站黄| 久久99精品久久久野外观看| 精品在线网站观看| 欧美黑人做爰爽爽爽| 国产在线一区不卡| 日韩一区三区| 欧美日韩国产免费观看视频| 中文久久精品| 91麻豆精品| 亚洲黄色免费av| 亚洲网站视频| 蜜桃视频第一区免费观看| 亚洲tv在线| 麻豆91在线播放| 久久精品av| 日韩精品一二区| 在线一区二区三区视频| 日韩中文字幕| 欧美日一区二区在线观看| 日韩精品三级| 91亚洲国产高清| 中文日韩在线| 精品中文在线| 麻豆久久精品| 亚洲欧洲美洲av| 婷婷综合电影| 亚洲人成在线网站| 亚洲资源网站| 高清一区二区三区| 亚洲一区二区成人| 国内精品伊人| 亚洲我射av| 久久免费影院| 亚洲精品在线a| 欧美aa在线观看| 成人午夜精品| 中文字幕乱码亚洲无线精品一区| 精品一区二区三区免费看 | 亚洲综合在线电影| 中文字幕一区二区av| 日韩网站中文字幕| 久久av导航| 日韩精品亚洲专区| av亚洲免费| 国产99在线| 亚洲ww精品| 蜜芽一区二区三区| 欧美精品激情| 亚洲性色视频| 特黄特色欧美大片| 国产一区二区三区91| 九九综合在线| 欧美13videosex性极品| 欧美综合精品| 91精品丝袜国产高跟在线| 婷婷丁香综合| 国产精品99免费看| 亚洲午夜精品久久久久久app| 青青青免费在线视频| 国产videos久久| 日本视频一区二区| 国产麻豆一区二区三区| 亚洲资源网站| 欧美片网站免费| 国产美女撒尿一区二区| 色婷婷色综合| 福利在线免费视频| 欧美精品高清| 久久国产免费| 欧美xxxx性| 亚洲黄色免费av| 国产一区日韩一区| 欧美日韩国产探花| 亚洲综合不卡| 欧美亚洲专区| 欧美日韩一区二区三区在线电影| 18国产精品| 国产成人精品一区二区免费看京| 国内一区二区三区| 免费观看久久av| 六月天综合网| 国产欧美日韩亚洲一区二区三区| 精品一区二区三区视频在线播放 | 欧美国产91| 亚洲精品人人| 亚洲精品大全| 亚洲影视一区二区三区| 国产精品观看| caoporn视频在线| 中文字幕乱码亚洲无线精品一区| 欧美91在线| 中文久久精品| 成人一区而且| 玖玖玖国产精品| 久久永久免费| 日本 国产 欧美色综合| 精品成av人一区二区三区| 99亚洲视频| 精品一区二区三区免费看| 久久午夜精品| 亚洲永久av| 国产高清亚洲| 蜜桃视频一区二区三区| 国产精品天天看天天狠| 欧美sss在线视频| 视频在线在亚洲| 狠狠躁少妇一区二区三区| 日本精品久久| 久久性天堂网| 一区久久精品| 亚洲香蕉网站| av免费不卡国产观看| 国产精品久久久久久久久久久久久久久 | 成人精品高清在线视频| 91精品福利观看| 亚洲区欧美区| 日本成人精品| 自拍日韩欧美| 尤物网精品视频| 狠狠色狠狠色综合日日tαg| 99精品视频在线| 欧美xxxx中国| 成人日韩av| 久久国产婷婷国产香蕉| 亚洲尤物av| 日韩午夜高潮| 亚洲在线观看| 国产亚洲欧洲| 亚洲午夜免费| 日韩欧美中文字幕在线视频| 亚洲三级毛片| 久久国产麻豆精品| 精品久久不卡| 精品亚洲美女网站| 欧美精品一区二区久久| 亚洲一区二区毛片| 在线看片日韩| 欧美日韩亚洲在线观看| 色在线中文字幕| 久久精品观看| 亚洲欧洲一区二区天堂久久| 91精品观看| 四虎国产精品免费久久| 国产伦理久久久久久妇女| 国产精品1区在线| 天堂中文av在线资源库| 99热精品在线观看| 国产精品极品在线观看| 亚洲精品高潮| 精品一区二区三区在线观看视频 | 日本免费一区二区视频| 久久精品凹凸全集| 日韩精品91| 精品精品99| 99国产精品免费视频观看| 亚洲在线一区| 日韩国产一区二区| 亚洲a成人v| 成人啊v在线| 日本中文字幕一区二区视频| 国产拍在线视频| 久久国产精品免费一区二区三区| 亚洲成人av观看| 国产精品国码视频| 亚洲大片在线| 91亚洲一区| 国产精品一线天粉嫩av| 中文一区二区| 日韩不卡在线| 国产精品网在线观看| 亚洲一区二区网站| 久久人人精品| 精品国产成人| 亚洲欧洲美洲国产香蕉| 精品九九久久| 91高清一区| 四虎4545www国产精品| 国产成人77亚洲精品www| 欧美精品97| 久久亚洲一区| 亚洲免费黄色| 91精品福利| 偷拍欧美精品| 四虎8848精品成人免费网站| 日本成人手机在线| 亚洲精品精选| 亚洲精品三级| 日韩精品久久久久久| 亚洲精选成人| 国产高清一区二区| 欧美在线网站| 老司机精品久久| 亚洲毛片一区| 91麻豆精品| 日本不卡在线视频|