springboot拦截器中注入service失败问题

本文探讨了在Spring框架中,正确配置拦截器并避免直接使用new创建Bean的重要性。通过实例说明了如何在配置类中自动注入拦截器,确保服务层能够被正确调用。

一般都是因为除了在拦截器之外,还需要在拦截器的配置类中,注册拦截器时没有使用spring的bean,而是使用了new创建bean造成的。

 

可参考:http://blog.youkuaiyun.com/wmh13262227870/article/details/77005920

当然,部分版本也支持以下方式:

@Configurationpublic 
class WebInterceptorLoader extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter{
//这里可以自动注入下interceptor

     @Autowired

private UserAuthInterceptor userAuthInterceptor;


@Override
public void addInterceptors(InterceptorRegistry registry){

//注意这里不要使用 new UserAuthInterceptor() ,否则就会出现拦截器里无法注入service的问题 
registry.addInterceptor(userAuthInterceptor).addPathPatterns("/**").excludePathPatterns("/components/*/*","/user-auth/quit");
}
}

转载于:https://www.cnblogs.com/pomfor/p/10411945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值