@ServletComponentScan 注解加上后拦截器失效

今天项目中遇到一个问题, 当使用@ServletComponentScan注解标注时, 原来正常的自定义MyInterceptor突然失效了,项目代码如下;
MyInterceptor 代码如下

public class MyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("check MyInterceptor................");
        HandlerMethod method = (HandlerMethod) handler;
        if (method == null) return true;
        if (method.getMethod().isAnnotationPresent(xxx.class)) {
            return true;
        }else{
            System.out.println("check UserLogin");
        }
        return false;
    }
...
...
}

Configuration配置文档如下

@Configuration
public class ZcWebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
    }
}

@ServletComponentScan 配置如下

@SpringBootApplication
@ServletComponentScan
public class ApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

具体原因暂时不知道,
结论是引入@ServletComponentScan时会扫描标注@WebFilter的自定义filter, 并且加载到项目中,
但是springboot项目中自定义的拦截器会被屏蔽. 所以只能改用使用@Configuration,配置注册为Bean的方式实现自定义filter

自定义filter使用详见本人另一个博客
https://blog.csdn.net/yangsnow_rain_wind/article/details/81326782

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值