springboot拦截器注入service为null【踩坑记录】

以下这种方式能注册拦截器,也能使用,但是获取到注入的service 是null。
在这里插入图片描述

我的拦截器类

@Component
public class LesionInterceptor implements HandlerInterceptor {

	@Autowired
	private IOrganizationService organizationService;
	
	// TODO 其他代码
}

配置类

/**
 * spring boot 2.0以上版本
 * https://blog.csdn.net/qq_35706771/article/details/82957014
 *
 * @author Jmx
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

	@Bean
	public MyWebMvcConfig getMyWebMvcConfig() {
		MyWebMvcConfig myWebMvcConfig = new MyWebMvcConfig() {
			@Override
			public void addViewControllers(ViewControllerRegistry registry) {
				// registry.addViewController("/").setViewName("alogin");
				// registry.addViewController("/login").setViewName("alogin");
				// registry.addViewController("/main.html").setViewName("dashboard");
			}

			// 注册拦截器
			@Override
			public void addInterceptors(InterceptorRegistry registry) {
				registry.addInterceptor(new LesionInterceptor())
						.addPathPatterns("/**")
						.excludePathPatterns("/login", "/swagger", "/",
								"/swagger-resources/**", "/webjars/**",
								"/v2/**", "/swagger-ui.html/**");
				super.addInterceptors(registry);
			}
		};
		return myWebMvcConfig;
	}
}

改进方式

/**
 * spring boot 2.0以上版本
 * https://blog.csdn.net/qq_35706771/article/details/82957014
 *
 * @author Jmx
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

	//这里的LesionInterceptor 是上面定义的拦截器的类型
	@Bean
	public LesionInterceptor MyWebMvcConfig() {
//		MyWebMvcConfig myWebMvcConfig = new MyWebMvcConfig() {
//			@Override
//			public void addViewControllers(ViewControllerRegistry registry) {
//				// registry.addViewController("/").setViewName("alogin");
//				// registry.addViewController("/login").setViewName("alogin");
//				// registry.addViewController("/main.html").setViewName("dashboard");
//			}
//
//			// 注册拦截器
//			@Override
//			public void addInterceptors(InterceptorRegistry registry) {
//				registry.addInterceptor(new LesionInterceptor())
//						.addPathPatterns("/**")
//						.excludePathPatterns("/login", "/swagger", "/",
//								"/swagger-resources/**", "/webjars/**",
//								"/v2/**", "/swagger-ui.html/**");
//				super.addInterceptors(registry);
//			}
//		};
		return new LesionInterceptor();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//多个拦截器组成一个拦截器链
		// addPathPatterns用于添加拦截规则
		// excludePathPatterns用户排除拦截
		//对来自/** 全路径请求进行拦截
		registry.addInterceptor(MyWebMvcConfig()).addPathPatterns("/**");

	}

}

在这里插入图片描述
至此解决,
还有一种方式就是继承 WebMvcConfigurerAdapter,但是这种方式已经过时。可参考 https://blog.csdn.net/wmh13262227870/article/details/77005920

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值