新建项目

这个是pom文件

<properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

新建FirstListener类

FirstListener.java
package com.gongspringlister.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* springBoot 整合 Listener
* *
<listener>
* <listener-class>com.bjsxt.listener.FirstListener</listener-class>
*</listener>
*/
@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) { } @Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Listener...init......");
}
}

新建App.java启动类

App.java

package com.gongspringlister;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; /**
* springBoot 整合 Listener 方式一
** *
*/
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args){
SpringApplication.run(App.class,args);
}
}

运行启动类

 SpringBoot 整合 Listener 方式二

新建SecondListener类

SecondListener.java

package com.gongspringlister.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* springBoot 整合 Listener 方式二
* * *
*/
@WebListener
public class SecondListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) { } @Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Listener...init......");
}
}

编写启动类App2.java

App2.java

package com.gongspringlister;

import com.gongspringlister.listener.SecondListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean; /**
* SpringBoot 整合 Listener 方式二* * *
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args){
SpringApplication.run(App2.class,args);
} /**
* 注册 listener
*/
@Bean
public ServletListenerRegistrationBean<SecondListener>
getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>
(new SecondListener());
return bean;
}
}

运行启动类App2.java

springBoot整合Listener的更多相关文章

  1. 02-03:springboot 整合listener

    1.通过注解扫描完成Listener组件的注册 1.1 编写listener /** * Springboot 整合listener */ import javax.servlet.ServletCo ...

  2. 【使用篇二】SpringBoot整合Listener(3)

    两种方式: 通过注解扫描完成 Listener 组件的注册 通过方法完成 Listener 组件注册 一.通过注解扫描完成 Listener 组件的注册 1. 编写Listener类 /*** spr ...

  3. 【SpringBoot】05.SpringBoot整合Listener的两种方式

    SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 创建一个类实现ServletContextListener (具体实现哪个Listener根据情况 ...

  4. SpringBoot学习4:springboot整合listener

    整合方式一:通过注解扫描完成 Listener 组件的注册 1.编写listener package com.bjsxt.listener; import javax.servlet.ServletC ...

  5. SpringBoot: 4.SpringBoot整合listener(转)

    整合方式一:通过注解扫描完成 Listener 组件的注册 1.编写listener package com.bjsxt.listener; import javax.servlet.ServletC ...

  6. SpringBoot整合三大组建(Servlet、Listener、Filter)

    >[更多资源和教程请关注公众号:**非科班的科班**.如果觉得我写的还可以请给个赞,谢谢大家,你的鼓励是我创作的动力](https://blog.csdn.net/qq_43255017)## ...

  7. SpringBoot整合JavaWeb

    一.SpringBoot整合Servlet的两种方式 1.通过注解扫描完成Servlet组件的注册 编写Servlet package com.example.demo.servlet; import ...

  8. springboot整合web开发(整合servlet、filter、listener、访问静态、文件上传)

    整合servlet 1.继承HttpServlet 2.添加@WebServlet注解 @WebServlet(name="FirstServlet",urlPatterns=&q ...

  9. SpringBoot整合RabbitMQ-整合演示

    本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...

随机推荐

  1. pat 甲级 1045 ( Favorite Color Stripe ) (动态规划 )

    1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She ...

  2. 顺序表应用1:多余元素删除之移位算法(SDUT 3324)

    Problem Description 一个长度不超过10000数据的顺序表,可能存在着一些值相同的"多余"数据元素(类型为整型),编写一个程序将"多余"的数据 ...

  3. c 判断一个字符是否为空格

    #include <stdio.h> #include <wctype.h> int main () { wchar_t c; ; wchar_t str[] = L" ...

  4. Mysql索引查询失效的情况

    首先,复习一下索引的创建: 普通的索引的创建: CREATE INDEX  (自定义)索引名  ON  数据表(字段); 复合索引的创建: CREATE INDEX  (自定义)索引名  ON  数据 ...

  5. Leetcode题目139.单词拆分(动态规划-中等)

    题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词.你可以假设字典 ...

  6. Remote Ubuntu VM from Windows

    Need to install the xrdp tool on Ubuntu. To do this, open a Terminal window (Ctrl + Alt + T) and ent ...

  7. 使用 Jenkins 发布 web 应用至 Tomcat

    Jenkins 的安装:https://www.cnblogs.com/jhxxb/p/11406805.html 发布流程:Git -> Maven -> Tomcat 一.基本环境 G ...

  8. POJ 1837 -- Balance(DP)

     POJ 1837 -- Balance 转载:優YoU   http://user.qzone.qq.com/289065406/blog/1299341345 提示:动态规划,01背包 初看此题第 ...

  9. ios-tableViewcell展开与收缩动画处理

    [前言] 在使用华尔街见闻 app 时,看到它的 tableVeiw 上的 cell 具有很好的展开与收缩功能.于是自己想了一下实现,感觉应该挺简单的,于是心痒痒写个 demo 实现一波.华尔街见闻 ...

  10. vscode通过服务器打开html文件

    之前讲到,安装open in brower插件之后可以直接右键选择使用浏览器渲染html文件,但是,这种打开方式是“本地打开方式” 那么我们怎么以服务的方式运行html呢: 1.下载插件:Live S ...