SpringBoot学习4:springboot整合listener
2024-09-04 13:03:15
整合方式一:通过注解扫描完成 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;
}
}
运行启动类,监听器初始化输出的信息就会在控制台输出
最新文章
- 用友U8 归纳采购退货结算三种情况
- 小div布局之卡片堆叠(card-stacking)
- Android中AsyncTask分析--你所不注意的坑
- rbd snap(1)
- ACCP7.0-S2-复习自测-15测试分析
- java类加载过程
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(八)-- 多环境开发
- JQUERY EASYUI 验证框(VALIDATEBOX)用法
- jq实现搜索引擎的提示效果
- ACM——五位以内的对称素数
- java.lang.Exception: Socket bind failed 服务器端口冲突-->;修改端口
- 如何解决This system is not registered with RHN.
- 深入理解CSS盒模型
- Gitlab的安装及项目新建
- kubernetes入门(06)kubernetes的核心概念(3)
- 【伯乐在线】这些 Git 技能够你用一年了
- 记录vue项目上线遇到的一些问题
- 洛谷P1966 【火柴排队】
- busybox linux-2.6.2 编译安装中碰到的若干问题
- Mac OS X 显示和隐藏文件
热门文章
- 73th LeetCode Weekly Contest Custom Sort String
- win10卸载更新+关闭自动更新的方法
- Unity PlayerPrefs
- (转)Bash Shell常用快捷键
- 【Linux】linux压缩和解压缩命令大全
- static修饰的类属性
- 16.Ubuntu LTS 16.04安装搜狗输入法全过程记录(纯新手)
- [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
- 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.
- 系统整理 精讲 swift 泛型