springboot拦截器@resource注解注入为null解决方案 拦截适配配置
springboot拦截器@resource注解注入为null解决方案 拦截适配配置
为什么@resource注入为null inteceptor在springcontext之前加载,注入必然是null
解决方案 加入注解@Bean,注意需要使用@Configuration,而不是@Component 解决 在Spring添加拦截器之前先自己创建一下这个Spring Bean,这样就能在Spring映射这个拦截器前,把拦截器中的依赖注入给完成了。
代码语言:javascript代码运行次数:0运行复制@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);
}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-01-15,如有侵权请联系 cloudcommunity@tencent 删除注解null解决方案配置依赖注入发布者:admin,转转请注明出处:http://www.yc00.com/web/1754973600a5223374.html
评论列表(0条)