Springboot配置过滤器实现过程解析
程序员文章站
2022-03-07 18:56:25
写一个继承类启动器扫一下此时访问一下页面就可以观察到过滤器的信息代码package com.example.demo.filter;import lombok.extern.slf4j.slf4j;i...
写一个继承类
启动器扫一下
此时访问一下页面就可以观察到过滤器的信息
代码
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); } }
结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。