整合方式一:通过注解扫描完成 Listener 组件的注册

1、编写listener

package com.bjsxt.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; /**
* Created by Administrator on 2019/2/5.
*/
@WebListener
public class FirstListener implements ServletContextListener{ @Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("FirstListener初始化......");
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { }
}

2、编写启动类

package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; /**
* Created by Administrator on 2019/2/4.
*/
@SpringBootApplication
@ServletComponentScan //在springboot启动时,会扫描@WebServlet、@WebFilter、@WebListener等,并将该类实例化
public class App { public static void main(String[] args){
SpringApplication.run(App.class,args);
}
}

整合方式二:通过方法完成 Listener 组件注册

1、编写listener

package com.bjsxt.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; /**
* Created by Administrator on 2019/2/5.
*/
public class SecondListener implements ServletContextListener{ @Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("SecondListener初始化......");
} @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { }
}

2、编写启动类

package com.bjsxt;

import com.bjsxt.filter.SecondFilter;
import com.bjsxt.listener.SecondListener;
import com.bjsxt.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; /**
* Created by Administrator on 2019/2/4.
*/
@SpringBootApplication
public class App2 { public static void main(String[] args){
SpringApplication.run(App2.class,args);
} /**
* 注册Servlet
* @return
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
} /**
* 注册Filter
* @return
*/
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
bean.addUrlPatterns(new String[]{"/second"});
return bean;
} /**
* 注册Listener
* @return
*/
@Bean
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}

运行启动类,监听器初始化输出的信息就会在控制台输出

SpringBoot学习4:springboot整合listener的更多相关文章

  1. SpringBoot学习- 4、整合JWT

    SpringBoot学习足迹 1.Json web token(JWT)是为了网络应用环境间传递声明而执行的一种基于JSON的开发标准(RFC 7519),该token被设计为紧凑且安全的,特别适用于 ...

  2. SpringBoot学习- 3、整合MyBatis

    SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...

  3. SpringBoot学习- 5、整合Redis

    SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...

  4. SpringBoot学习- 8、整合Shiro

    SpringBoot学习足迹 Shiro是什么,引自百度百科:Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快 ...

  5. springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置

    一.整合Druid数据源 Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库 ...

  6. springboot学习笔记-3 整合redis&mongodb

    一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...

  7. springboot学习四:整合mybatis

    在application.properties加入配置 ## Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain my ...

  8. springboot学习三:整合jsp

    在pom.xml加入jstl <!--springboot tomcat jsp 支持开启--> <dependency> <groupId>org.apache. ...

  9. SpringBoot学习(八)-->SpringBoot之过滤器、监听器

    本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...

  10. SpringBoot学习(六)-->SpringBoot的自动配置的原理

    Spring Boot的自动配置的原理 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入 ...

随机推荐

  1. VUE中嵌套路由

    官网地址:https://router.vuejs.org/zh-cn/essentials/nested-routes.html 路由嵌套一般使用在后台管理系统中 给一个比较简单的小案例 <! ...

  2. spark_20180328

    // 2.1 条件表达式val x = 2val s = if (x > 0) 1 else -1if (x > 0) "positive" else -1// 返回值 ...

  3. Juniper srx 550建立NAT端口映射

    一.Juniper srx 550建立NAT端口映射 公司Juniper srx 550路由器,因为很少去设置,所以怕到时设置时步骤又给忘记了,这里做个备注,以便日后查 NAT配置界面介绍: Rule ...

  4. D. Little Artem and Dance

    题目链接:http://codeforces.com/problemset/problem/669/D D. Little Artem and Dance time limit per test 2 ...

  5. ${openid_wx} el解析式放入url的“”里才起作用。

    window.location.href="${ctx }/wx/reservation/content?shopid="+shopid+"&&openi ...

  6. discuz迁移到虚拟空间后无法上传图片的问题

    discuz X3迁移到虚拟空间后无法上传图片,提示"附件无法保存": 解决方法: 1.看看虚拟空间的容量是不是满了. 2.登录管理员后台,工具->更新缓存.

  7. sql 删除字段 出错

    1. 删除字段:   ALTER TABLE TALE_NAME DROP COLUMN nn;   2.报错: 消息 5074,级别 16,状态 1,第 1 行对象'DF__WorkOrder__I ...

  8. 一起来学Spring Cloud | 第一章 :如何搭建一个多模块的springcloud项目

    在spring cloud系列章节中,本来已经写了几个章节了,但是自己看起来有些东西写得比较杂,所以重构了一下springcloud的章节内容,新写了本章节,先教大家在工作中如何搭建一个多模块的spr ...

  9. python中函数的定义与调用

    1.为什么要用函数? (1)代码重复太多(2)可读性差 使用函数的好处: (1)代码重用 (2)保持一致性,易维护 (2)可扩展性 2.初始函数定义与调用     函数的定义 def test(x): ...

  10. CF1136D Nastya Is Buying Lunch

    思路: 1. 最终答案不超过能与Nastya“直接交换”的人数. 2. 对于排在j前面的i,如果i和i-j之间(包括j)的每个人都能“直接交换”,j才能前进一步. 实现: #include <b ...