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

Stella981
• 阅读 521

      前面都说到了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 {

Logger log = Logger.getLogger(MyServletContextListener.class);

@Override
public void sessionCreated(HttpSessionEvent se) {
    log.info(">>>>>>>>>session被创建>>>>>>>>>");
}

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    log.info(">>>>>>>>session被销毁>>>>>>>>>");
}

}

二.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 {

Logger log = Logger.getLogger(MyServletContextListener.class);

@Override
public void contextInitialized(ServletContextEvent arg) {
    log.info(">>>>>>>>>ServletContex初始化>>>>>>>>>");
}

@Override
public void contextDestroyed(ServletContextEvent arg) {
    log.info(">>>>>>>>>ServletContex销毁>>>>>>>>>");
}

}

三.增加注解

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 {

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

}

启动项目测试:

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

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
SpringBoot 注册Servlet三大组件【Servlet、Filter、Listener】
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。注册三大组件,classMyServlet()/classMyListener/classMyFilter需要自己亲自编写!!1\.classMyServletpa
Stella981 Stella981
2年前
Spring Boot使用笔记
1\. boot将自动把bean类型为Servlet,Filter,listener等servlet规范中的类型,向boot内嵌的web容器注册,需要控制filter、servlet的初始化和参数等,可以使用FilterRegistrationBean和ServletRegistrationBean,来自文档https://docs.spring.i
Stella981 Stella981
2年前
Spring Boot系列之 入门应用
以后几天书写一个SpringBoot系列的内容小小最近学习SpringBoot体系中SpringBoot一种化繁为简的一种体系。把原先复杂的SSM,替换为简单的SpringBoot一键式启动。什么是Springboot其实Springboot是Spring家族中的一个全新的框架,它是用来简单应用程序的创建
Easter79 Easter79
2年前
SpringBoot学习之路:10.Spring Boot中添加Listener应用
   前面都说到了springboot中应用servlet和filter,本文继续说springboot应用监听器Listener,大体上是和之前差不多的应用。一.HttpSessionListenerpackagecom.maxbill.core.webbox.listener;/\\
Stella981 Stella981
2年前
SpringBoot学习之路:09.Spring Boot中添加Filter应用
   上篇文章中说了SpringBoot中是如何使用servlet的,本文将讲解在SpringBoot中对过滤器Filter的实现一.编写MyFilter实现Filter接口packagecom.maxbill.core.webbox.filter;importorg.apache.log4j
Easter79 Easter79
2年前
SpringBoot学习之路:08.Spring Boot中添加Servlet应用
   在web应用中Servlet的应用比较多,最开始的web应用就是全部以servlet来实现的,后来出现了Struts、Struts2,到今天应用非常广泛的SpringMvc,这些webmvc框架比原来的servlet功能更加强大,开发效率而更高,但是实际上这些框架底层都是servlet在做支撑。今天我们学习如何在SpringBoot添加Servl
Stella981 Stella981
2年前
Spring Boot Servlet支持介绍
Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到Servlet、Filter、Listener、Interceptor 等等。当使用SpringBoot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。 
Stella981 Stella981
2年前
SpringBoot学习之路:08.Spring Boot中添加Servlet应用
   在web应用中Servlet的应用比较多,最开始的web应用就是全部以servlet来实现的,后来出现了Struts、Struts2,到今天应用非常广泛的SpringMvc,这些webmvc框架比原来的servlet功能更加强大,开发效率而更高,但是实际上这些框架底层都是servlet在做支撑。今天我们学习如何在SpringBoot添加Servl
Stella981 Stella981
2年前
Eclipse搭建springboot项目(八)拦截器、过滤器、监听器
知识点:1、SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter(核心知识)  filter简单理解:人检票员(filter)景点  1)SpringBoot启动默认加载的Filter    characterEncodingFilter    
Easter79 Easter79
2年前
SpringBoot学习之路:09.Spring Boot中添加Filter应用
   上篇文章中说了SpringBoot中是如何使用servlet的,本文将讲解在SpringBoot中对过滤器Filter的实现一.编写MyFilter实现Filter接口packagecom.maxbill.core.webbox.filter;importorg.apache.log4j