springboot 配置過濾器
阿新 • • 發佈:2020-07-31
寫一個繼承類
啟動器掃一下
此時訪問一下頁面就可以觀察到過濾器的資訊
程式碼
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); } }