随笔 - 547  文章 - 1  评论 - 293  阅读 - 33万

springboot拦截器@resource注解注入为null解决方案 拦截适配配置

springboot拦截器@resource注解注入为null解决方案  拦截适配配置

为什么@resource注入为null
inteceptor在springcontext之前加载,注入必然是null

解决方案
加入注解@Bean,注意需要使用@Configuration,而不是@Component
解决
在Spring添加拦截器之前先自己创建一下这个Spring Bean,这样就能在Spring映射这个拦截器前,把拦截器中的依赖注入给完成了。

复制代码
@Configuration
public class UserWebMVCConfigurer extends WebMvcConfigurerAdapter {
    /**
     * @return UserInterceptor
     */
    @Bean
    public UserInterceptor userInterceptor() {
        return  new UserInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userInterceptor())
        .addPathPatterns("/**")
        .excludePathPatterns("/public/login/*","/error");
        super.addInterceptors(registry);
    }
}
复制代码

 

posted on   oktokeep  阅读(964)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2022-01-15 Excel表格Vlookup跨sheet取值,ISNA函数处理匹配不到的空字符串
2022-01-15 如何查询Windows10的激活时间等相关信息
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示