SpringBoot使用过滤器、拦截器和监听器的案例代码(Springboot搭建java项目)

 更新时间:2023年02月02日 10:54:06   作者:dreamer_0423  
这篇文章主要介绍了SpringBoot使用过滤器、拦截器和监听器(Springboot搭建java项目),本文是基于Springboot搭建java项目,结合案例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

SpringBoot使用过滤器、拦截器和监听器

一、SpringBoot使用过滤器

Spring boot过滤器的使用(两种方式)

  • 使用spring boot提供的FilterRegistrationBean注册Filter
  • 使用原生servlet注解定义Filter

两种方式的本质都是一样的,都是去FilterRegistrationBean注册自定义Filter

方式一:

第一步:先定义Filter。

import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // do something 处理request 或response
        System.out.println("filter1");
        // 调用filter链中的下一个filter
        filterChain.doFilter(servletRequest,servletResponse);
    }
    @Override
    public void destroy() {
    }
}

第二步:注册自定义Filter

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean registrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(1);//定义过滤器的执行先后顺序  值越小越先执行 不影响Bean的加载顺序
        return filterRegistrationBean;
    }
}

方式二:

// 注入spring容器
@Order(1)//定义过滤器的执行先后顺序  值越小越先执行 不影响Bean的加载顺序
@Component
// 定义filterName 和过滤的url
@WebFilter(filterName = "my2Filter" ,urlPatterns = "/*")
public class My2Filter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filter2");
    }
    @Override
    public void destroy() {
    }
}

二、SpringBoot使用拦截器

第一步:定义拦截器

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

第二步:配置拦截器

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }
}

三、过滤器和拦截器的执行顺序

过滤器的执行顺序是安装@Order注解中的值,或者是setOrder()中值的进行执行顺序排序的,值越小就越靠前。

拦截器则是先声明的拦截器 preHandle() 方法先执行,而postHandle()方法反而会后执行。也即是:postHandle() 方法被调用的顺序跟 preHandle() 居然是相反的。如果实际开发中严格要求执行顺序,那就需要特别注意这一点。

四、SpringBoot使用监听器

1、统计网站最多在线人数监听器的例子

/**
 * 上下文监听器,在服务器启动时初始化onLineCount和maxOnLineCount两个变量,
 * 并将其置于服务器上下文(ServletContext)中,其初始值都是0。
 */
@WebListener
public class InitListener implements ServletContextListener {
    public void contextDestroyed(ServletContextEvent evt) {
    }
    public void contextInitialized(ServletContextEvent evt) {
        evt.getServletContext().setAttribute("onLineCount", 0);
        evt.getServletContext().setAttribute("maxOnLineCount", 0);
    }
}
/**
 * 会话监听器,在用户会话创建和销毁的时候根据情况修改onLineCount和maxOnLineCount的值。
 */
@WebListener
public class MaxCountListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent event) {
        ServletContext ctx = event.getSession().getServletContext();
        int count = Integer.parseInt(ctx.getAttribute("onLineCount").toString());
        count++;
        ctx.setAttribute("onLineCount", count);
        int maxOnLineCount = Integer.parseInt(ctx.getAttribute("maxOnLineCount").toString());
        if (count > maxOnLineCount) {
            ctx.setAttribute("maxOnLineCount", count);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ctx.setAttribute("date", df.format(new Date()));
        }
    }
    public void sessionDestroyed(HttpSessionEvent event) {
        ServletContext app = event.getSession().getServletContext();
        int count = Integer.parseInt(app.getAttribute("onLineCount").toString());
        count--;
        app.setAttribute("onLineCount", count);
    }
}

新建一个servlet处理

@WebServlet(name = "SessionServlet",value = "/sessionCount")
public class SessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        //获取上下文对象
        ServletContext servletContext = this.getServletContext();
        Integer onLineCount = (Integer) servletContext.getAttribute("onLineCount");
        System.out.println("invoke doGet");
        PrintWriter out = resp.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + onLineCount + "</h1>");
        out.println("</body></html>");
    }
}

2、springboot监听器的使用(以实现异步Event监听为例子)

定义事件类 Event

创建一个类,继承ApplicationEvent,并重写构造函数。ApplicationEvent是Spring提供的所有应用程序事件扩展类。

public class Event extends ApplicationEvent {
    private static final long serialVersionUID = 1L;
    private String msg ;
    private static final Logger logger=LoggerFactory.getLogger(Event.class);
    public Event(String msg) {
        super(msg);
        this.msg = msg;
        logger.info("add event success! message: {}", msg);
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

创建一个用于监听指定事件的类,需要实现ApplicationListener接口,说明它是一个应用程序事件的监听类。注意这里需要加上@Component注解,将其注入Spring容器中。

@Component
public class MyListener implements ApplicationListener<Event>{
    private static final Logger logger= LoggerFactory.getLogger(MyListener.class);
    @Override
    public void onApplicationEvent(Event event) {
        logger.info("listener get event,sleep 2 second...");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("event msg is:{}",event.getMsg());
    }
}

事件发布
事件发布很简单,只需要使用Spring 提供的ApplicationEventPublisher来发布自定义事件

@Autowired 注入ApplicationEventPublisher

@RequestMapping("/notice/{msg}")
    public void notice(@PathVariable String msg){
        logger.info("begin>>>>>");
        applicationEventPublisher.publishEvent(new Event(msg));
        logger.info("end<<<<<<<");
    }

到此这篇关于SpringBoot使用过滤器、拦截器和监听器(Springboot搭建java项目)的文章就介绍到这了,更多相关SpringBoot使用过滤器、拦截器和监听器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java多线程中线程的两种创建方式及比较代码示例

    Java多线程中线程的两种创建方式及比较代码示例

    这篇文章主要介绍了Java多线程中线程的两种创建方式及比较代码示例,简单介绍了线程的概念,并行与并发等,然后通过实例代码向大家展示了线程的创建,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • 可视化Swing中JTable控件绑定SQL数据源的两种方法深入解析

    可视化Swing中JTable控件绑定SQL数据源的两种方法深入解析

    以下是对可视化Swing中JTable控件绑定SQL数据源的两种方法进行了详细的分析介绍,需要的朋友可以过来参考一下
    2013-07-07
  • Java JWT实现跨域身份验证方法详解

    Java JWT实现跨域身份验证方法详解

    JWT(JSON Web Token)是目前流行的跨域认证解决方案,是一个开放标准(RFC 7519),它定义了一种紧凑的、自包含的方式,用于作为JSON对象在各方之间安全地传输信息。本文将介绍JWT如何实现跨域身份验证,感兴趣的可以学习一下
    2022-01-01
  • springMVC详细介绍

    springMVC详细介绍

    下面小编就为大家带来一篇基于Spring MVC 详细介绍。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-07-07
  • spring 定时任务@Scheduled详解

    spring 定时任务@Scheduled详解

    这篇文章主要介绍了spring 定时任务@Scheduled的相关资料,文中通过示例代码介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
    2017-01-01
  • Maven 项目用Assembly打包可执行jar包的方法

    Maven 项目用Assembly打包可执行jar包的方法

    这篇文章主要介绍了Maven 项目用Assembly打包可执行jar包的方法,该方法只可打包非spring项目的可执行jar包,需要的朋友可以参考下
    2023-03-03
  • Java 方法的重载与参数传递详解

    Java 方法的重载与参数传递详解

    在java中,方法就是用来完成解决某件事情或实现某个功能的办法。方法实现的过程中,会包含很多条语句用于完成某些有意义的功能——通常是处理文本,控制输入或计算数值,这篇文章我们来探究一下方法的重载与传参
    2022-04-04
  • SpringBoot整合Redis之编写RedisConfig

    SpringBoot整合Redis之编写RedisConfig

    RedisConfig需要对redis提供的两个Template的序列化配置,所以本文为大家详细介绍了SpringBoot整合Redis如何编写RedisConfig,需要的可以参考下
    2022-06-06
  • 为什么rest接口返回json建议采用下划线形式,不要用驼峰

    为什么rest接口返回json建议采用下划线形式,不要用驼峰

    为什么rest接口返回json建议采用下划线形式,不要用驼峰?今天小编就来为大家说明一下原因,还等什么?一起跟随小编过来看看吧
    2020-09-09
  • idea mybatis配置log4j打印sql语句的示例

    idea mybatis配置log4j打印sql语句的示例

    本篇文章主要介绍了idea mybatis配置log4j打印sql语句的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01

最新评论