Spring實現(xiàn)內(nèi)置監(jiān)聽器
Spring內(nèi)置監(jiān)聽器
對于 Web 應用來說,ServletContext 對象是唯一的,一個 Web 應用,只有一個ServletContext 對象,該對象是在 Web 應用裝載時初始化的。若將 Spring 容器的創(chuàng)建時機,放在 ServletContext 初始化時,就可以保證 Spring 容器的創(chuàng)建只會執(zhí)行一次,也就保證了Spring 容器在整個應用中的唯一性。
當 Spring 容器創(chuàng)建好后,在整個應用的生命周期過程中,Spring 容器應該是隨時可以被訪問的。即,Spring 容器應具有全局性。而放入 ServletContext 對象的屬性,就具有應用的全局性。所以,將創(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文件中注冊監(jiān)聽器
若要在 ServletContext 初 始 化 時 創(chuàng) 建 Spring 容 器 , 就 需 要 使 用 監(jiān) 聽 器 接 口ServletContextListener 對 ServletContext 進行監(jiān)聽。在 web.xml 中注冊該監(jiān)聽器
Spring 為該監(jiān)聽器接口定義了一個實現(xiàn)類 ContextLoaderListener,完成了兩個很重要的工作:創(chuàng)建容器對象,并將容器對象放入到了 ServletContext 的空間中。打開 ContextLoaderListener 的源碼。看到一共四個方法,兩個是構(gòu)造方法,一個初始化方法,一個銷毀方法
<!--注冊監(jiān)聽器 ContextLoaderListener--> <!-- 監(jiān)聽器被創(chuàng)建對象后,會讀取/WEB-INF/applicationContext.xml 可以修改默認的文件位置,使用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)建了容器對象context,并且將context對象加入到了servletContext全局作用域?qū)ο笾?,key值為WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
獲取容器對象
1、直接通過key值獲取
WebApplicationContext context = null; Object attr = getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (attr != null){ context = (WebApplicationContext)attr; }
2、通過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é)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java中l(wèi)ogback?自動刷新不生效的問題解決
本文主要介紹了Java中l(wèi)ogback?自動刷新不生效的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05SpringBoot+Thymeleaf靜態(tài)資源的映射規(guī)則說明
這篇文章主要介紹了SpringBoot+Thymeleaf靜態(tài)資源的映射規(guī)則說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Java使用NIO優(yōu)化IO實現(xiàn)文件上傳下載功能
IO 是基于流來讀取的,而NIO則是基于塊讀取,面向流 的 I/O 系統(tǒng)一次一個字節(jié)地處理數(shù)據(jù),這篇文章主要介紹了Java使用NIO優(yōu)化IO實現(xiàn)文件上傳下載功能,需要的朋友可以參考下2022-07-07Java SpringMVC的@RequestMapping注解使用及說明
這篇文章主要介紹了Java SpringMVC的@RequestMapping注解使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Java中List、Set、Map的區(qū)別和實現(xiàn)方式示例代碼
這篇文章主要介紹了Java中List、Set、Map的區(qū)別和實現(xiàn)方式示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06java注解結(jié)合aspectj AOP進行日志打印的操作
這篇文章主要介紹了java注解結(jié)合aspectj AOP進行日志打印的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02