解決在Interceptor攔截器中使用@DubboReference注入為null

lingFei_y發表於2020-10-16

只能注入攔截器,而不是new,不能手動new,否則dubbo服務注入不進去,就變成了null了

攔截器配置類

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {   
@Autowired
  JurisdictionInterceptor jurisdictionInterceptor;
  
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
  	//註冊TestInterceptor攔截器,  只能注入攔截器,而不是new,不能手動new,否則dubbo服務注入不進去
  	InterceptorRegistration registration = registry.addInterceptor(jurisdictionInterceptor);
  	registration.addPathPatterns("/**");  //所有路徑都被攔截
  	registration.excludePathPatterns(                         //新增不攔截路徑
  			
  	);
  }
}

攔截器

@Component
public class JurisdictionInterceptor implements HandlerInterceptor {

	@DubboReference
	private UserService userService;

	/**
	 * @Description:  在請求處理之前進行呼叫(Controller方法呼叫之前) 
	 **/
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
		
		return true;
	}
}

相關文章