1. 程式人生 > 其它 >Spring內建監聽器

Spring內建監聽器

技術標籤:Web後端+框架spring

對於 Web 應用來說,ServletContext 物件是唯一的,一個 Web 應用,只有一個ServletContext 物件,該物件是在 Web 應用裝載時初始化的。若將 Spring 容器的建立時機,放在 ServletContext 初始化時,就可以保證 Spring 容器的建立只會執行一次,也就保證了Spring 容器在整個應用中的唯一性。

當 Spring 容器建立好後,在整個應用的生命週期過程中,Spring 容器應該是隨時可以被訪問的。即,Spring 容器應具有全域性性。而放入 ServletContext 物件的屬性,就具有應用的全域性性。所以,將建立好的 Spring 容器,以屬性的形式放入到 ServletContext 的空間中,就保證了 Spring 容器的全域性性。

上述的這些工作,已經被封裝在瞭如下的 Spring 的 Jar 包的相關 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檔案中註冊監聽器

若要在 ServletContext 初 始 化 時 創 建 Spring 容 器 , 就 需 要 使 用 監 聽 器 接 口ServletContextListener 對 ServletContext 進行監聽。在 web.xml 中註冊該監聽器

Spring 為該監聽器介面定義了一個實現類 ContextLoaderListener,完成了兩個很重要的工作:建立容器物件,並將容器物件放入到了 ServletContext 的空間中。開啟 ContextLoaderListener 的原始碼。看到一共四個方法,兩個是構造方法,一個初始化方法,一個銷燬方法

<!--註冊監聽器 ContextLoaderListener-->
<!--
    監聽器被建立物件後,會讀取/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()方法的部分原始碼,可以看到在該方法中建立了容器物件context,並且將context物件加入到了servletContext全域性作用域物件中,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中的呼叫關係可以清晰的獲得

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);