微服务,微架构[十二]springboot监听器,过滤器
来源:互联网 发布:四柱排盘软件下载 编辑:程序博客网 时间:2024/06/08 13:30
一、概要
springboot 中的监听器与过滤器都会依赖于 原生态的servlet,其实都是万般不离其中 最终采用都是底层servlet的处理加载,很多时候spring对已有的内容进行更深层次的封装,方便我们使用和处理,避免操作原生态数据和接口,在springboot中提供两种方式,一种是采用原生态的方式处理,另外一种采用springboot自己封装方式处理,底层还是需要实现servlet原生态接口
二、注解描述
@ServletComponentScan 当使用嵌入式容器时,可以使用自动注册的@ webservlet、@ webfilter和@ weblistener注释类,需要配置到启动application.java启动入口中进行全局扫描
@WebListener 嵌入是的写法相当于使用web.xml配置的监听类,进行初始化
@ webfilter 嵌入是的写法相当于使用web.xml配置的过滤器,进行初始化
FilterRegistrationBean springboot提供了以创建类的形式进行添加filter,最终会被ServletContextInitializer进行初始化添加到springioc容器中
ServletListenerRegistrationBean 提供了以创建类的形式进行添加Listener,最终会被ServletContextInitializer进行初始化添加到springioc容器中
三、代码示例
方案1:
1、启动类
@SpringBootApplication@ServletComponentScanpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
2、过滤器
/* * 全局过滤 * 方法1:需要在服务器动进行自动扫描自动注解 @ServletComponentScan 否则不能加载到spring容器中,必能被启用 */@WebFilter(urlPatterns = "/*")public class GlobalFilter implements Filter { private Logger log = LoggerFactory.getLogger(GlobalFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("GlobalFilter - Filter init ..."); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { log.info("GlobalFilter - Filter doFilter ..."); } @Override public void destroy() { log.info("GlobalFilter - Filter destroy ..."); }}
3、监听器
/* * 全局监听器 * 方法1:需要在服务器动进行自动扫描自动注解 @ServletComponentScan 否则不能加载到spring容器中,必能被启用 */@WebListenerpublic class GlobalListener implements ServletContextListener { private Logger log = LoggerFactory.getLogger(GlobalListener.class); @Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub log.info("系统已启动。。。。。。"); } @Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub log.info("系统已关闭。。。。。。"); }}
以上代码关注这几个@WebListener,@WebFilter,启动扫描注解配置@ServletComponentScan
方案2:
1、启动类
@SpringBootApplicationpublic class SpringbootBeanApplication { public static void main(String[] args) { SpringApplication.run(SpringbootBeanApplication.class, args); }}
2、过滤器,监听器同一个地方配置加载
/** * 不需要任何注解,springboot容器启动时候会自动加载初始ben加载容器中,不需要指定扫描@ServletComponentScan注解 * * @ClassName: SpringbootWeb * @date: 2017年5月31日 上午11:04:01 * @version: V1.0 * */@Configurationpublic class SpringbootWeb { /** * springboot 自定义注册Filter * * @Title: indexFilterRegistration * @return * */ @Bean public FilterRegistrationBean indexFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(new SpringbootWebFilter()); // 初始化顺序 registration.setOrder(0); // 初始化过滤连接 List<String> urlPatterns = new ArrayList<String>(); urlPatterns.add("/*"); // 设置滤过率的请求 registration.setUrlPatterns(urlPatterns); return registration; } /** * spring boot 自定义监听器 * * @Title: servletListenerRegistrationBean * @return * */ @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean() { ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new SpringbootWebListener()); return servletListenerRegistrationBean; }}
过滤器:
public class SpringbootWebFilter implements Filter { private Logger log = LoggerFactory.getLogger(SpringbootWebFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("SpringbootWebFilter - filter init......"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub log.info("SpringbootWebFilter - filter doFilter......"); chain.doFilter(request, response); } @Override public void destroy() { // TODO Auto-generated method stub log.info("SpringbootWebFilter - filter destroy......"); }}监听器
/** * spring boot 实现ServletContextListener接口,启动自动初始化加载 * * @ClassName: SpringbootWebListener * @date: 2017年5月31日 上午11:07:39 * @version: V1.0 * */public class SpringbootWebListener implements ServletContextListener { private Logger log = LoggerFactory.getLogger(SpringbootWebListener.class); @Override public void contextInitialized(ServletContextEvent sce) { log.info("SpringbootWebListener - listener contextInitialized ....."); } @Override public void contextDestroyed(ServletContextEvent sce) { log.info("SpringbootWebListener - listener contextDestroyed ....."); }}
以上就是两种不同的方案解决办法,请大家关注,评论
阅读全文
0 0
- 微服务,微架构[十二]springboot监听器,过滤器
- SpringBoot开始微服务架构
- 微服务,微架构[一]之springboot[helloWorld]
- 微服务,微架构[二]之springboot集成jdbc
- 微服务,微架构[四]之springboot集成Redis缓存
- 微服务,微架构[五]之springboot读取properties文件
- 微服务,微架构[六]之springboot集成mybatis
- 微服务,微架构[零]springboot入门介绍
- 微服务,微架构[七]之springboot异常处理
- 微服务,微架构[八]springboot多环境切换profiles
- 微服务,微架构[十]springboot模板页面freemarker
- 微服务,微架构[十一]springboot模板页面velocity
- 微服务,微架构[十一]springboot模板页面thymeleaf
- 微服务,微架构[十三]springboot集成RabbitMQ
- SpringBoot快速构建微服务架构体系
- 基于SpringBoot的微服务架构实践
- 十三、SpringBoot配置过滤器、监听器、Servlets
- springboot之过滤器,监听器的使用
- 史上最全的常用开发工具类收集
- 网页抓取工具之数据预处理
- unix 网络编程
- Java集合类汇总
- 117. Populating Next Right Pointers in Each Node II
- 微服务,微架构[十二]springboot监听器,过滤器
- Hadoop 集群搭建(单机/伪分布式/分布式)
- 机器学习:数据驱动的科学
- Java的内存回收机制
- 集合-Set
- 全军出击,银联出台标准二维码产品
- System.arraycopy 与 Arrays.copyOf 分析
- Excel封装类 导成excel 封装思路
- 个人中心,设置头像