当前位置: 首页 > 科技观察

SpringBoot监听器的使用方法

时间:2023-03-12 08:14:42 科技观察

前言监听器(Listener)就是监听对象的创建、销毁等状态变化,定义某些事件发生后下一步要执行的动作。主要监控的三个域对象是:ServletRequest域、HttpSession域和ServletContext域。本文通过几个简单的例子来介绍监听器的使用。ServletContextListener监听Servlet上下文。Servlet上下文对象可以在系统启动时初始化一些数据,方便使用时直接调用。监听器的实现代码如下:@ComponentpublicclassMyServletContextListenerimplementsServletContextListener{@Override//创建ServletContext对象后立即调用进行初始化publicvoidcontextInitialized(ServletContextEventevent){event.getServletContext().setAttribute("name","九天银河聊天编程");System.out.println("ServletContext对象创建完成");}@Override//在ServletContext对象被销毁后调用publicvoidcontextDestroyed(ServletContextEventevent)。System.out.println("ServletContext对象被销毁");}}@GetMapping("/getServletContext")publicStringgetServletContext(HttpServletRequestrequest){ServletContextservletContext=request.getServletContext();对象名称=servletContext.getAttribute("name");返回String.valueOf(名称);}执行效果如下:HttpSessionListener获取在线人数@ComponentpublicclassMyHttpSessionListenerimplementsHttpSessionListener{publicstaticIntegercount=0;@OverridepublicvoidsessionCreated(HttpSessionEvent事件){count++;ServletContextapplication=event.getSession().getServletContext();application.setAttribute("用户数",计数);System.out.println("有人在线,现在在线人数为:"+count+"person");}@OverridepublicvoidsessionDestroyed(HttpSessionEventevent){count--;ServletContextapplication=event.getSession().getServletContext();application.setAttribute("用户数",计数);System.out.println("有人离线,现在在线人数为:"+count+"person");}}@GetMapping("/online")publicStringgetOnlinePersoncount(HttpServletRequestrequest){IntegeruserCount=(Integer)request.getServletContext().getAttribute("用户数");return(userCount==null?"0":userCount+"");}@GetMapping("/login")publicStringlogged(HttpSessionsession){//相同的session,如果sessionid相同,则只监听一次session.setAttribute("用户名","九天银河聊天编程");return"success";}@GetMapping("/logout")publicStringlogout(HttpSessionsession){session.invalidate();//设置session无效return"success";}执行127.0.0.1:8090/login,控制台显示:执行127.0.0.1:8090/online,返回。执行127.0.0.1:8090/logout,控制台显示。ServletRequestListener统计网站访问次数@ComponentpublicclassMyServletRequestListenerimplementsServletRequestListener{@OverridepublicvoidrequestInitialized(ServletRequestEventservletRequestEvent){ObjectcountObject=servletRequestEvent.getServletContext().getAttribute){ObjectcountObject=servletRequestEvent.getServletContext().getAttribute);+计数对象);整数计数=0;如果(countObject!=null)count=Integer.valueOf(countObject.toString());计数++;servletRequestEvent.getServletContext().setAttribute("计数",计数);}@OverridepublicvoidrequestDestroyed(ServletRequestEventservletRequestEvent){System.out.println("当前访问次数:"+servletRequestEvent.getServletContext().getAttribute("count"));}}随机执行一个接口请求,控制台打印如下:再次执行:自定义监听方法定义监听事件publicclassListenerEventextendsApplicationEvent{Stringname=null;publicStringgetName(){返回名称;}publicvoidsetName(字符串名称){this.name=名称;}publicListenerEvent(Objectsource,Stringvalue){super(source);名字=价值;}}定义监听器@ComponentpublicclassMySpringBootListenerimplementsApplicationListener{@OverridepublicvoidonApplicationEvent(ListenerEventlistenerEvent){StringeventInfo=listenerEvent.getName();System.out.println(事件信息);}}发布事件@RestControllerpublicclassListenerController{@ResourceprivateApplicationContextapplicationContext;@GetMapping("/listener")publicStringlistener(){ListenerEventevent=newListenerEvent(this,"九天银河聊天编程");applicationContext.publishEvent(事件);返回””;}}执行127.0.0.1:8090/listener,控制台显示Listener说明目前ServletAPI中提供的web事件监听接口如下:ServletContextListener--监听servletContext对象的创建和销毁contextInitialized(ServletContextEventevent)--创建时执行contextDestroyed(ServletContextEventevent)--销毁ExecuteHttpSessionListener--监听会话对象的创建和销毁sessionCreated(HttpSessionEventevent)--创建时执行sessionDestroyed(HttpSessionEventevent)--销毁时执行ServletRequestListener--监听请求对象的创建和销毁requestInitialized(ServletRequestEventevent)--创建时执行requestDestroyed(ServletRequestEventevent)--销毁时执行ServletContextAttributeListener--监听servletContext对象中属性的变化attributeAdded(ServletContextAttributeEventevent)--添加属性时执行attributeReplaced(ServletContextAttributeEventevent)es--修改属性时执行attributeRemoved删除属性时执行HttpSessionAttributeListener--监听session对象中属性的变化attributeAdded(HttpSessionBindingEventevent)--添加属性时执行attributeReplaced(HttpSessionBindingEventevent)--修改时执行attributeRemoved(HttpSessionBindingEventevent)属性ttpSessionBindingEvent事件)--删除属性时执行ServletRequestAttributeListener--监听请求对象中属性的变化attributeAdded(ServletRequestAttributeEvent事件)--添加属性时执行attributeReplaced(ServletRequestAttributeEvent事件)--修改属性时执行attributeRemoved(ServletRequest属性)执行时生命周期请求指的是URL请求,发送请求时创建,返回响应时销毁。问。session失效有以下几种情况:session过期,即用户长时间不访问服务器,导致过期用户退出系统,即执行session的invalidate方法,并清理会议。当前web应用被卸载(session不被持久化)applicationrunningthroughthecurrent在WEB应用的生命周期中,application对象在当前WEB应用加载时被创建,application对象在当前WEB应用被销毁时被销毁已卸载。