`
huangyongxing310
  • 浏览: 490314 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Spring 监听器

阅读更多
Spring 监听器


1.监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。
2.监听器其实就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法立即被执行。


java的事件监听机制
1、事件监听涉及到三个组件:事件源、事件对象、事件监听器
2、当事件源上发生某一个动作时,它会调用事件监听器的一个方法,并在调用该方法时把事件对象传递进去,开发人员在监听器中通过事件对象,就可以拿到事件源,从而对事件源进行操作。


ServletRequestListener
用于监听用户请求的到达,实现该接口的监听器需要实现如下两个方法。
requestInitialized(ServletRequestEvent sre):用户请求到底、被初始化时触发该方法。
requestDestroyed(ServletRequestEvent sre):用户请求结束、被销毁时触发该方法。


ServletRequestAttributeListener
1.用于监听ServletRequest(request)范围内属性的变化,实现该接口的监听器需要实现attributeAdded、attributeRemoved、attributeReplaced三个方法。
2.ServletRequestAttributeListener与ServletContextAttributeListener的作用相似,都用于监听属性的改变,只是ServletRequestAttributeListener监听request范围内属性的改变,而ServletContextAttributeListener监听的是application范围内属性的改变。


HttpSessionListener
用于监听用户session的创建和销毁,实现该接口的监听器需要实现如下两个方法。
sessionCreated(HttpSessionEvent se):用户与服务器的会话开始、创建时时触发该方法。
sessionDestroyed(HttpSessionEvent se):用户与服务器的会话断开、销毁时触发该方法。


HttpSessionAttributeListener
1.用于监听HttpSession(session)范围内属性的变化,实现该接口的监听器需要实现attributeAdded、attributeRemoved、attributeReplaced三个方法。
2.HttpSessionAttributeListener与ServletContextAttributeListener的作用相似,都用于监听属性的改变,只是HttpSessionAttributeListener监听session范围内属性的改变,而ServletContextAttributeListener监听的是application范围内属性的改变。


ServletContextListener
当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,实现该接口的监听器需要实现如下两个方法。
contextInitialized(ServletContextEvent sce) :当Servlet 容器启动Web 应用时调用该方法。在调用完该方法之后,容器再对Filter 初始化,并且对那些在Web 应用启动时就需要被初始化的Servlet 进行初始化。
contextDestroyed(ServletContextEvent sce) :当Servlet 容器终止Web 应用时调用该方法。在调用该方法之前,容器会先销毁所有的Servlet 和Filter 过滤器。


ServletContextAttributeListener
ServletContextAttributeListener用于监听ServletContext(application)范围内属性的变化,实现该接口的监听器需要实现如下三个方法。
attributeAdded(ServletContextAttributeEvent event):当程序把一个属性存入application范围时触发该方法。
attributeRemoved(ServletContextAttributeEvent event):当程序把一个属性从application范围删除时触发该方法。
attributeReplaced(ServletContextAttributeEvent event):当程序替换application范围内的属性时将触发该方法。


例子:
1、编写监听器
package com.proserver.common.controller.Test.Listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyHttpSessionListener  implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		// TODO Auto-generated method stub
		System.out.println( se.getSession() + "sessionCreated 创建了!!");
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		// TODO Auto-generated method stub
		System.out.println("sessionDestroyed 销毁了!!");
	}
}



package com.proserver.common.controller.Test.Listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		System.out.println("contextInitialized 对象创建");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		System.out.println("contextDestroyed 对象销毁");
	}
}



package com.proserver.common.controller.Test.Listener;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class MyServletRequestListener implements ServletRequestListener {

	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		// TODO Auto-generated method stub
		System.out.println(sre.getServletRequest() + "requestDestroyed 销毁了!!");
	}

	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		// TODO Auto-generated method stub
		System.out.println(sre.getServletRequest() + "requestInitialized 创建了!!");
	}
}



2、在web.xml文件中注册监听器
<listener>
	<description>HttpSessionListener监听器</description>
	<listener-class>com.proserver.common.controller.Test.Listener.MyServletContextListener</listener-class>
</listener>
<listener>
	<description>HttpSessionListener监听器</description>
	<listener-class>com.proserver.common.controller.Test.Listener.MyHttpSessionListener</listener-class>
</listener>
<session-config>
	<session-timeout>1</session-timeout>
</session-config>
<listener>
	<description>ServletRequestListener监听器</description>
	<listener-class>com.proserver.common.controller.Test.Listener.MyServletRequestListener</listener-class>
</listener>



参考原文:http://book.51cto.com/art/201104/253574.htm
参考原文:http://blog.csdn.net/niuch1029291561/article/details/7867735
分享到:
评论

相关推荐

    spring监听器共20页.pdf.zip

    Spring监听器是Spring框架中的一个重要组成部分,主要用于监听和响应应用上下文或Bean的生命周期事件。在Spring中,监听器是通过实现特定接口或者继承抽象类来定义的,这些接口包括ApplicationContextAware、...

    spring 监听器浅析.docx

    本文将对Spring监听器进行深入浅析,包括其工作原理、源码解析以及如何使用。 首先,Spring监听器是基于事件驱动模型的,这种模式在多线程和分布式系统中非常常见。在Spring中,当一个特定的事件发生时,如bean的...

    spring监听器

    ### Spring监听器与过滤器详解:关键代码与实践应用 #### 标题解析与核心概念 标题“spring监听器”指向了Spring框架中一个重要的组件——监听器(Listener)。Spring框架不仅在Java企业级开发中提供了强大的依赖...

    Spring监听器及定时任务实现方法详解

    Spring监听器及定时任务实现方法详解 在本文中,我们将详细介绍Spring监听器及定时任务实现方法的详解。通过示例代码,我们将展示如何使用Spring监听器和定时任务来实现批处理任务的执行。 Spring监听器 在Spring...

    Spring监听资料

    Spring监听器是实现特定接口的类,这些接口提供了对Spring容器生命周期事件的处理能力。本资料集合将深入探讨Spring监听器的概念、作用以及如何在实际开发中使用它们。 首先,我们要了解的是`ApplicationListener`...

    使用Spring事件机制实现异步的方法

    使用@EventListener注解也可以轻松地实现事件监听器,Spring框架将自动扫描带有该注解的类,并将其添加到ApplicationContext中。在上面的代码中,使用@EventListener注解的MyEventHandler类将被Spring框架自动添加到...

    spring的监听器和缓存.docx

    在Spring框架中,监听器和缓存是两个重要的概念,它们在系统运行时起到关键的作用。监听器允许我们对Web应用程序的生命周期事件进行监听和处理,而缓存则用于优化性能,减少数据库交互。 首先,让我们关注监听器。...

    Spring boot通过HttpSessionListener监听器统计在线人数的实现代码

    在Spring boot中通过HttpSessionListener监听器统计在线人数是一种常见的技术实现方式,适用于需要跟踪和管理用户会话状态的Web应用程序。以下是从给定文件中提炼的关键知识点。 首先,了解HttpSessionListener接口...

    监听器Listener

    这是一个重要的Spring监听器,负责初始化Spring的Web应用上下文。通过在web.xml中配置,它可以确保在Web应用启动时加载Spring的根应用上下文。 5. **CharacterEncodingFilter**: 虽然不是一个监听器,但通常与...

    spring 事件监听 3种方式

    本篇文章将详细介绍Spring事件监听的三种方式:基于接口的监听器、基于类的监听器以及基于注解的监听器。 ### 1. 基于接口的监听器(ApplicationListener) **接口定义:** Spring提供了`ApplicationListener`接口...

    spring框架技术+第2天+xmind思维导图

    spring框架技术+第2天+xmind思维导图:生命周期,介绍simple project,打印出构造方法...bean作用域request session globalSession:web项目获取核心配置文件要配置两个地方:spring监听器、spring作用域范围的监听。

    struts加载spring的方法

    2. **配置Spring监听器**:在`web.xml`文件中,需要配置一个Spring的上下文监听器`ContextLoaderListener`,该监听器负责初始化Spring的ApplicationContext。具体配置如下所示: ```xml &lt;listener-class&gt;org....

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    【Spring在Web项目中的应用】 ...同时,通过监听器`ContextLoaderListener`,可以在Web容器启动时自动加载Spring配置,确保在整个Web应用程序生命周期中,Service层和其他Spring管理的bean都可以正确地被创建和管理。

    FreeMarker整合Spring_3

    -- Spring监听器 --&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener &lt;!-- Spring上下文参数 --&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;classpath*:...

    Struts整合Spring步骤

    - **配置Spring监听器**:在`web.xml`中添加Spring的ContextLoaderListener,以便启动时加载Spring上下文。 - **使用Struts 2 Spring插件**:添加`struts2-spring-plugin-2.0.12.jar`,这样Struts 2可以自动识别和...

    Struts2与Spring之间的整合方法与原理

    - **配置Spring监听器**:在`web.xml`中添加`ContextLoaderListener`监听器,它会在Web应用启动时加载Spring的ApplicationContext。 - **Spring配置文件**:通常,主Spring配置文件是`applicationContext.xml`,...

    Spring的ApplicationEvent事件和监听器的测试Demo

    在Spring框架中,ApplicationEvent和监听器是实现应用内异步通信的重要机制。这个测试Demo主要展示了如何在Spring环境中创建自定义事件、发布事件以及注册和处理这些事件。以下是对这个测试工程的详细解析: 首先,...

    Spring的监听器ContextLoaderListener的作用

    Spring 的监听器 ContextLoaderListener 的作用 ContextLoaderListener 是 Spring 框架中的一种监听器,它的主要作用是启动 Web 容器时,自动装配 ApplicationContext 的配置信息。它实现了 ServletContextListener...

Global site tag (gtag.js) - Google Analytics