Dependency injection in HandlerInterceptor of SpringBoot is an null problem

  • 2021-11-14 05:53:39
  • OfStack

Directory SpringBoot HandlerInterceptor Dependency Injection for null Cause Solution spring Dependency Injection Object for null Annotated Object as follows

SpringBoot HandlerInterceptor dependency injection for null

Cause

Interceptor loading is completed before springcontext is created

Solutions

Use @ Bean to load classes before interceptor initialization

1. Load the interception class in the custom subclass of WebMvcConfigurer, as follows:


@Configuration
public class ApIAppConfigurer implements WebMvcConfigurer {
    /**
     *  Inject custom interception classes into spring Container 
     * @return
     */
    @Bean
    public ApiInterceptorAdapter getMyInterceptor(){
        return  new ApiInterceptorAdapter();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getMyInterceptor()) // Specify the interceptor class 
                .addPathPatterns("/api/**"); // Object that this class intercepts url
    }
}

2. Use @ Component to hand over the interception class to the spring container, as follows:


@Component
public class ApiInterceptorAdapter extends HandlerInterceptorAdapter {
    @Autowired
    private IApiTokenService iApiTokenService;
    }

3. Complete the above two steps and you can inject service through @ Autowired.

The spring dependency injection object is null

Not long ago, I helped a colleague debug a piece of code and found that the injected object was null

The annotated objects are as follows


@Component
  public class SparkSource{
@Autowired
  private SparkConfig  sparkConfig  ;  
@Autowired  
  private RedisUtils redisUtils; 

Injection is used when calling SparkSource


@Component
public class Owner{
@Autowired
private SparkSource sparkSource;

Then when using SparkSource object, 1 directly reports null; At first, I thought it was injection failure. Breakpoint debugging found that RedisUtils in SparkSource object was actually null, which indicated that either injection was unsuccessful or initialization was not carried out.

Modify the default construct and start the log discovery declaration bean to be successful. That is, there was a problem when injecting, and then 1 went straight to find the reason in Owner and noticed that the object itself was declared as an bean component.

Then he jumped out of Owner and found that his initial call actually got the object in the way of new Owner ():


Owner owner = new Owner();

At this time, I finally found the problem. Amend to read:


@Autowired
private Owner owner ;

When an object is declared as an bean component, it is managed by the spring container, which will help you initialize it; However, if the new method is used to call an object, it will skip the spring container to generate a new object, which cannot be initialized at this time, so it will appear that the SparkSource object is null during debugging, and the object referenced by injection in the SparkSource object is also null;; Components declared as bean objects must be called using injection.

This is an spring dependency injection novice is easy to ignore a problem, 1 will not pay attention to, I hope you can pay more attention when writing code.

The description in this article may not be detailed enough. If you are free, you can learn more about spring dependency injection and inversion of control.


Related articles: