好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring实现内置监听器

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文件中加入依赖

?

1

2

3

4

5

< 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 的源码。看到一共四个方法,两个是构造方法,一个初始化方法,一个销毁方法

?

1

2

3

4

5

6

7

8

9

10

11

12

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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值获取

?

1

2

3

4

5

WebApplicationContext context = null;

Object attr = getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

if (attr != null){

     context = (WebApplicationContext)attr;

}

2、通过WebApplicationContextUtils工具类获取

以下是 WebApplicationContextUtils 中的调用关系可以清晰的获得

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

?

1

2

ServletContext sc = getServletContext();

WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);

总结

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

原文链接:https://blog.csdn.net/qq_45796208/article/details/112604741

查看更多关于Spring实现内置监听器的详细内容...

  阅读:18次

上一篇: 详细了解MVC+proxy

下一篇:springMVC详细介绍