如何使用监听器

监听器概述

1.Listener是Servlet的监听器 

2.可以监听客户端的请求、服务端的操作等。

3.通过监听器,可以自动激发一些操作,如监听在线用户数量,当增加一个HttpSession时,给在线人数加1。

4.编写监听器需要实现相应的接口

5.编写完成后在web.xml文件中配置一下,就可以起作用了

6.可以在不修改现有系统基础上,增加web应用程序生命周期事件的跟踪

常用的监听接口

1.ServletContextAttributeListener

监听对ServletContext属性的操作,比如增加/删除/修改

2.ServletContextListener

监听ServletContext,当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。 3.HttpSessionListener

监听HttpSession的操作。当创建一个Session时,激发session Created(SessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。

4.HttpSessionAttributeListener

监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

使用范例:

由监听器管理共享数据库连接

生命周期事件的一个实际应用由context监听器管理共享数据库连接。在web.xml中如下定义监听器: 
<listener>
    <listener-class>XXX.MyConnectionManager</listener-class>
</listener>server

创建监听器的实例,接受事件并自动判断实现监听器接口的类型。要记住的是由于监听器是配置在部署描述符web.xml中,所以不需要改变任何代码就可以添加新的监听器。 
public class MyConnectionManager implements ServletContextListener{  
public void contextInitialized(ServletContextEvent e) { 
Connection con = // create connection 
e.getServletContext().setAttribute("con", con); 
}  
public void contextDestroyed(ServletContextEvent e) { 
Connection con = (Connection) e.getServletContext().getAttribute("con"); 
try {
con.close(); 

catch (SQLException ignored) { } // close connection 

}

转载于:https://www.cnblogs.com/raymond19840709/archive/2008/08/28/1278710.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用监听器可以通过实现`ApplicationListener`接口来实现。下面是一个简单的示例: 首先,创建一个自定义监听器类,实现`ApplicationListener`接口,并指定监听的事件类型。例如,我们创建一个监听器类`MyEventListener`来监听`ApplicationStartedEvent`事件: ```java import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; public class MyEventListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { System.out.println("应用程序已启动!"); // 在这里可以添加自定义的逻辑 } } ``` 然后,在Spring Boot应用程序的主类中注册监听器。可以使用`SpringApplication.addListeners()`方法来添加监听器: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(MyApplication.class); app.addListeners(new MyEventListener()); // 注册自定义监听器 app.run(args); } } ``` 这样,在应用程序启动时,`MyEventListener`中的`onApplicationEvent()`方法就会被调用。 除了`ApplicationStartedEvent`,Spring Boot还提供了其他一些常用的事件,例如`ApplicationReadyEvent`、`ContextRefreshedEvent`等,你可以选择适合你需求的事件类型来监听。 希望以上信息能对你有帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值