Spring實(shí)現(xiàn)內(nèi)置監(jiān)聽器
Spring內(nèi)置監(jiān)聽器
對于 Web 應(yīng)用來說,ServletContext 對象是唯一的,一個 Web 應(yīng)用,只有一個ServletContext 對象,該對象是在 Web 應(yīng)用裝載時初始化的。若將 Spring 容器的創(chuàng)建時機(jī),放在 ServletContext 初始化時,就可以保證 Spring 容器的創(chuàng)建只會執(zhí)行一次,也就保證了Spring 容器在整個應(yīng)用中的唯一性。
當(dāng) Spring 容器創(chuàng)建好后,在整個應(yīng)用的生命周期過程中,Spring 容器應(yīng)該是隨時可以被訪問的。即,Spring 容器應(yīng)具有全局性。而放入 ServletContext 對象的屬性,就具有應(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文件中注冊監(jiān)聽器
若要在 ServletContext 初 始 化 時 創(chuàng) 建 Spring 容 器 , 就 需 要 使 用 監(jiān) 聽 器 接 口ServletContextListener 對 ServletContext 進(jìn)行監(jiān)聽。在 web.xml 中注冊該監(jiān)聽器
Spring 為該監(jiān)聽器接口定義了一個實(shí)現(xiàn)類 ContextLoaderListener,完成了兩個很重要的工作:創(chuàng)建容器對象,并將容器對象放入到了 ServletContext 的空間中。打開 ContextLoaderListener 的源碼。看到一共四個方法,兩個是構(gòu)造方法,一個初始化方法,一個銷毀方法

<!--注冊監(jiān)聽器 ContextLoaderListener-->
<!--
監(jiān)聽器被創(chuàng)建對象后,會讀取/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)建了容器對象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線程池隊(duì)列LinkedTransferQueue示例詳解
這篇文章主要為大家介紹了Java線程池隊(duì)列LinkedTransferQueue示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
MybatisPlus調(diào)用原生SQL的三種方法實(shí)例詳解
這篇文章主要介紹了MybatisPlus調(diào)用原生SQL的三種方法,在有些情況下需要用到MybatisPlus查詢原生SQL,MybatisPlus其實(shí)帶有運(yùn)行原生SQL的方法,我這里列舉三種,需要的朋友可以參考下2022-09-09
SpringBoot手動開啟事務(wù):DataSourceTransactionManager問題
這篇文章主要介紹了SpringBoot手動開啟事務(wù):DataSourceTransactionManager問題,具有很好的價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境
本文主要介紹了Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

