亲宝软件园·资讯

展开

Springboot过滤器 Springboot配置过滤器实现过程解析

等不到 人气:0
想了解Springboot配置过滤器实现过程解析的相关内容吗,等不到在本文为您仔细讲解Springboot过滤器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Springboot,配置,过滤器,下面大家一起来学习吧。

写一个继承类

启动器扫一下

此时访问一下页面就可以观察到过滤器的信息

代码

package com.example.demo.Filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;

@Slf4j
@WebFilter(filterName = "myFilter1", urlPatterns = "/*")
public class MyFilter1 implements Filter {
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    log.info(filterConfig.getFilterName() + " init,过滤器初始化成功!");

  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    log.info("myFilter1 begin");
    try {
      log.info("业务方法执行");
      chain.doFilter(request, response);
    } catch (Exception e) {
      log.error("error!", e);
    }
    log.info("myFilter1 end");
  }

  @Override
  public void destroy() {
  }
}

启动器

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;


@SpringBootApplication(scanBasePackages = {"com.example"})
@MapperScan("com.example.demo.mapper")
@ServletComponentScan(basePackages = "com.example.demo.Filter")
public class Demo10Application {

  public static void main(String[] args) {
    SpringApplication.run(Demo10Application.class, args);
  }

}

结果

加载全部内容

相关教程
猜你喜欢
用户评论