Spring實(shí)現(xiàn)內(nèi)置監(jiān)聽(tīng)器
Spring內(nèi)置監(jiān)聽(tīng)器
對(duì)于 Web 應(yīng)用來(lái)說(shuō),ServletContext 對(duì)象是唯一的,一個(gè) Web 應(yīng)用,只有一個(gè)ServletContext 對(duì)象,該對(duì)象是在 Web 應(yīng)用裝載時(shí)初始化的。若將 Spring 容器的創(chuàng)建時(shí)機(jī),放在 ServletContext 初始化時(shí),就可以保證 Spring 容器的創(chuàng)建只會(huì)執(zhí)行一次,也就保證了Spring 容器在整個(gè)應(yīng)用中的唯一性。
當(dāng) Spring 容器創(chuàng)建好后,在整個(gè)應(yīng)用的生命周期過(guò)程中,Spring 容器應(yīng)該是隨時(shí)可以被訪問(wèn)的。即,Spring 容器應(yīng)具有全局性。而放入 ServletContext 對(duì)象的屬性,就具有應(yīng)用的全局性。所以,將創(chuàng)建好的 Spring 容器,以屬性的形式放入到 ServletContext 的空間中,就保證了 Spring 容器的全局性。
上述的這些工作,已經(jīng)被封裝在了如下的 Spring 的 Jar 包的相關(guān) API 中:spring-web-5.2.5.RELEASE
下面演示使用步驟
pom.xml文件中加入依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.5.RELEASE</version> </dependency>
在web.xml文件中注冊(cè)監(jiān)聽(tīng)器
若要在 ServletContext 初 始 化 時(shí) 創(chuàng) 建 Spring 容 器 , 就 需 要 使 用 監(jiān) 聽(tīng) 器 接 口ServletContextListener 對(duì) ServletContext 進(jìn)行監(jiān)聽(tīng)。在 web.xml 中注冊(cè)該監(jiān)聽(tīng)器
Spring 為該監(jiān)聽(tīng)器接口定義了一個(gè)實(shí)現(xiàn)類 ContextLoaderListener,完成了兩個(gè)很重要的工作:創(chuàng)建容器對(duì)象,并將容器對(duì)象放入到了 ServletContext 的空間中。打開(kāi) ContextLoaderListener 的源碼??吹揭还菜膫€(gè)方法,兩個(gè)是構(gòu)造方法,一個(gè)初始化方法,一個(gè)銷毀方法
<!--注冊(cè)監(jiān)聽(tīng)器 ContextLoaderListener--> <!-- 監(jiān)聽(tīng)器被創(chuàng)建對(duì)象后,會(huì)讀取/WEB-INF/applicationContext.xml 可以修改默認(rèn)的文件位置,使用context-param重新指定文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
try { if (this.context == null) { this.context = this.createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)this.context; if (!cwac.isActive()) { if (cwac.getParent() == null) { ApplicationContext parent = this.loadParentContext(servletContext); cwac.setParent(parent); } this.configureAndRefreshWebApplicationContext(cwac, servletContext); } } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
上面是initWebApplicationContext()方法的部分源碼,可以看到在該方法中創(chuàng)建了容器對(duì)象context,并且將context對(duì)象加入到了servletContext全局作用域?qū)ο笾?,key值為WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
獲取容器對(duì)象
1、直接通過(guò)key值獲取
WebApplicationContext context = null; Object attr = getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (attr != null){ context = (WebApplicationContext)attr; }
2、通過(guò)WebApplicationContextUtils工具類獲取
以下是WebApplicationContextUtils
中的調(diào)用關(guān)系可以清晰的獲得
public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) throws IllegalStateException { WebApplicationContext wac = getWebApplicationContext(sc); if (wac == null) { throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?"); } else { return wac; } } @Nullable public static WebApplicationContext getWebApplicationContext(ServletContext sc) { return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); } @Nullable public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) { Assert.notNull(sc, "ServletContext must not be null"); Object attr = sc.getAttribute(attrName);
ServletContext sc = getServletContext(); WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java中l(wèi)ogback?自動(dòng)刷新不生效的問(wèn)題解決
本文主要介紹了Java中l(wèi)ogback?自動(dòng)刷新不生效的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05SpringBoot+Thymeleaf靜態(tài)資源的映射規(guī)則說(shuō)明
這篇文章主要介紹了SpringBoot+Thymeleaf靜態(tài)資源的映射規(guī)則說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java使用NIO優(yōu)化IO實(shí)現(xiàn)文件上傳下載功能
IO 是基于流來(lái)讀取的,而NIO則是基于塊讀取,面向流 的 I/O 系統(tǒng)一次一個(gè)字節(jié)地處理數(shù)據(jù),這篇文章主要介紹了Java使用NIO優(yōu)化IO實(shí)現(xiàn)文件上傳下載功能,需要的朋友可以參考下2022-07-07Java SpringMVC的@RequestMapping注解使用及說(shuō)明
這篇文章主要介紹了Java SpringMVC的@RequestMapping注解使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Java中List、Set、Map的區(qū)別和實(shí)現(xiàn)方式示例代碼
這篇文章主要介紹了Java中List、Set、Map的區(qū)別和實(shí)現(xiàn)方式示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06SpringBoot實(shí)現(xiàn)登錄攔截的示例代碼
如果我們不進(jìn)行登錄攔截的話,即使我們跳過(guò)登錄頁(yè)面直接去訪問(wèn)任意一個(gè)頁(yè)面也能訪問(wèn)成功,那么登錄功能就沒(méi)有意義,同時(shí)也會(huì)存在安全問(wèn)題,本文就來(lái)介紹一下SpringBoot登錄攔截,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09java注解結(jié)合aspectj AOP進(jìn)行日志打印的操作
這篇文章主要介紹了java注解結(jié)合aspectj AOP進(jìn)行日志打印的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02