好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot的HandlerInterceptor中依赖注入为null问题

SpringBoot HandlerInterceptor依赖注入为null

原因

拦截器加载是在springcontext创建之前完成

解决方案

使用@Bean在拦截器初始化之前让类加载

1.在WebMvcConfigurer的自定义子类加载拦截类,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

@Configuration

public class ApIAppConfigurer implements WebMvcConfigurer {

     /**

      * 注入自定义拦截类到spring容器

      * @return

      */

     @Bean

     public ApiInterceptorAdapter getMyInterceptor(){

         return   new ApiInterceptorAdapter();

     }

     @Override

     public void addInterceptors(InterceptorRegistry registry) {

         registry.addInterceptor(getMyInterceptor()) //指定拦截器类

                 .addPathPatterns( "/api/**" ); //指定该类拦截的url

     }

}

2.使用@Component把拦截类交与spring容器管理,代码如下:

?

1

2

3

4

5

@Component

public class ApiInterceptorAdapter extends HandlerInterceptorAdapter {

     @Autowired

     private IApiTokenService iApiTokenService;

     }

3.完成上述两步就可以通过@Autowired 注入service了。

spring依赖注入对象为null

前不久帮一个同事调试一段代码,发现注入对象为null

被注解的对象如下

?

1

2

3

4

5

6

@Component

   public class SparkSource{

@Autowired

   private SparkConfig  sparkConfig  ; 

@Autowired

   private RedisUtils redisUtils;

在调用SparkSource时候使用了注入的方式

?

1

2

3

4

@Component

public class Owner{

@Autowired

private SparkSource sparkSource;

然后在使用SparkSource 对象的时候一直报null;刚开始以为是注入失败,断点调试发现SparkSource对象里面的RedisUtils居然也是为null,说明要不就是注入不成功,要不就是没有进行初始化。

修改默认构造,启动日志发现申明bean是成功的。那就是在注入的时候出现了问题,然后一直在Owner里面找原因,留意到其实这个对象本身也是被申明成一个bean组件。

然后跳出Owner,发现其实在他最开始的调用竟然是以new Owner()的方式来获取对象:

?

1

Owner owner = new Owner();

这时候终于找到问题的所在了。修改为:

?

1

2

@Autowired

private Owner owner ;

当对象声明为bean组件的时候,它是交给spring容器去管理的,容器会帮你进行初始化;但是如果使用new方法来调用对象时,会跳过spring容器生成新的对象,这时候就无法进行初始化,所以在调试的时候就会出现SparkSource对象为null,并且SparkSource对象里面以注入方式引用的对象也为null;被申明为bean对象的组件必须使用注入的方式进行调用。

这是一个spring依赖注入新手很容易忽视的一个问题,一般也不会去重视,希望大家在写代码的时候能多加留意。

本文描述可能不够详细,大家有空可以去了解一下更多的关于spring依赖注入与控制反转。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/dengdeng333/article/details/87878882

查看更多关于SpringBoot的HandlerInterceptor中依赖注入为null问题的详细内容...

  阅读:25次