欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring实现内置监听器

程序员文章站 2022-06-21 16:19:33
目录spring内置监听器2、通过webapplicationcontextutils工具类获取spring内置监听器对于 web 应用来说,servletcontext 对象是唯一的,一个 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 的源码。看到一共四个方法,两个是构造方法,一个初始化方法,一个销毁方法

Spring实现内置监听器

<!--注册监听器 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);

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

相关标签: Spring 监听器