SpringBoot学习之路:10.Spring Boot中添加Listener应用

      前面都说到了springboot中应用servlet和filter,本文继续说springboot应用监听器Listener,大体上是和之前差不多的应用。

一.HttpSessionListener

package com.maxbill.core.webbox.listener;

/** * @功能 * @作者 zuoshuai(MaxBill) * @日期 2017/7/6 * @时间 15:27 * @备注 MyHttpSessionListener */

import org.apache.log4j.Logger;

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

@WebListener public class MyHttpSessionListener implements HttpSessionListener {

1Logger log = Logger.getLogger(MyServletContextListener.class); 2 3@Override 4public void sessionCreated(HttpSessionEvent se) { 5 log.info(">>>>>>>>>session被创建>>>>>>>>>"); 6} 7 8@Override 9public void sessionDestroyed(HttpSessionEvent se) { 10 log.info(">>>>>>>>session被销毁>>>>>>>>>"); 11}

}

二.ServletContextListener

package com.maxbill.core.webbox.listener;

import com.maxbill.core.webbox.filter.MyFilter; import org.apache.log4j.Logger;

import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;

/** * @功能 自定义监听器 * @作者 zuoshuai(MaxBill) * @日期 2017/7/6 * @时间 15:24 * @备注 MyServletContextListener */ @WebListener public class MyServletContextListener implements ServletContextListener {

1Logger log = Logger.getLogger(MyServletContextListener.class); 2 3@Override 4public void contextInitialized(ServletContextEvent arg) { 5 log.info(">>>>>>>>>ServletContex初始化>>>>>>>>>"); 6} 7 8@Override 9public void contextDestroyed(ServletContextEvent arg) { 10 log.info(">>>>>>>>>ServletContex销毁>>>>>>>>>"); 11}

}

三.增加注解

1.在监听器上增加@WebListener

@WebListener public class MyHttpSessionListener implements HttpSessionListener {...}

@WebListener public class MyServletContextListener implements ServletContextListener {...}

2.然后在启动类中增加@ServletComponentScan,使项目中的Listener能够被扫描到

package com.maxbill;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan @SpringBootApplication public class MaxbillApplication {

1public static void main(String\[\] args) { 2 SpringApplication.run(MaxbillApplication.class, args); 3}

}

启动项目测试:

INFO com.maxbill.core.webbox.listener.MyServletContextListener:24 - >>>>>>>>>ServletContex初始化>>>>>>>>>

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

SpringBoot 注册Servlet三大组件【Servlet、Filter、Listener】

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。注册三大组件,classMyServlet()/classMyListener/classMyFilter需要自己亲自编写!!1\.classMyServletpa

Spring Boot使用笔记

1\. boot将自动把bean类型为Servlet,Filter,listener等servlet规范中的类型,向boot内嵌的web容器注册,需要控制filter、servlet的初始化和参数等,可以使用FilterRegistrationBean和ServletRegistrationBean,来自文档https://docs.spring.i

SpringBoot学习之路:09.Spring Boot中添加Filter应用

   上篇文章中说了SpringBoot中是如何使用servlet的,本文将讲解在SpringBoot中对过滤器Filter的实现一.编写MyFilter实现Filter接口packagecom.maxbill.core.webbox.filter;importorg.apache.log4j

SpringBoot学习之路:10.Spring Boot中添加Listener应用

   前面都说到了springboot中应用servlet和filter,本文继续说springboot应用监听器Listener,大体上是和之前差不多的应用。一.HttpSessionListenerpackagecom.maxbill.core.webbox.listener;/\\

SpringBoot学习之路:08.Spring Boot中添加Servlet应用

   在web应用中Servlet的应用比较多,最开始的web应用就是全部以servlet来实现的,后来出现了Struts、Struts2,到今天应用非常广泛的SpringMvc,这些webmvc框架比原来的servlet功能更加强大,开发效率而更高,但是实际上这些框架底层都是servlet在做支撑。今天我们学习如何在SpringBoot添加Servl