整合方式一:通过注解扫描完成 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;
}
}

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

最新文章

  1. 用友U8 归纳采购退货结算三种情况
  2. 小div布局之卡片堆叠(card-stacking)
  3. Android中AsyncTask分析--你所不注意的坑
  4. rbd snap(1)
  5. ACCP7.0-S2-复习自测-15测试分析
  6. java类加载过程
  7. 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(八)-- 多环境开发
  8. JQUERY EASYUI 验证框(VALIDATEBOX)用法
  9. jq实现搜索引擎的提示效果
  10. ACM——五位以内的对称素数
  11. java.lang.Exception: Socket bind failed 服务器端口冲突--&gt;修改端口
  12. 如何解决This system is not registered with RHN.
  13. 深入理解CSS盒模型
  14. Gitlab的安装及项目新建
  15. kubernetes入门(06)kubernetes的核心概念(3)
  16. 【伯乐在线】这些 Git 技能够你用一年了
  17. 记录vue项目上线遇到的一些问题
  18. 洛谷P1966 【火柴排队】
  19. busybox linux-2.6.2 编译安装中碰到的若干问题
  20. Mac OS X 显示和隐藏文件

热门文章

  1. 73th LeetCode Weekly Contest Custom Sort String
  2. win10卸载更新+关闭自动更新的方法
  3. Unity PlayerPrefs
  4. (转)Bash Shell常用快捷键
  5. 【Linux】linux压缩和解压缩命令大全
  6. static修饰的类属性
  7. 16.Ubuntu LTS 16.04安装搜狗输入法全过程记录(纯新手)
  8. [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
  9. Authentication to host &#39;***‘&#39; for user &#39;root&#39; using method &#39;mysql_native_password&#39; failed with message: Reading from the stream has failed.
  10. 系统整理 精讲 swift 泛型