The login interceptor cannot inject the StringRedisTemplate to update the expired token

Question leads to:

        When writing a login interceptor, we need to analyze the token's expiration time, start time and other information through the token to determine whether the token has expired. If the token expires, we need to update the token cache. Due to the redis used in my project, it is normal to use the @Autowired annotation to automatically inject StringRedisTemplate, but empty exceptions are frequently reported during use. After adding annotations such as @Component to the interceptor to let the interceptor be managed by spring, it was found that StringRedisTemplate still could not be injected. Later, debug looked at the execution process in detail, and found that StringRedisTemplate cannot be injected! But redis can be injected under other packages.

problem lies in:

        We know that Java component classes generally hand over this class to spring management through annotations. If it cannot be injected, then this class has not been injected into spring. When we inject a component class according to the normal steps in the past, it still cannot be injected. Then it may be a problem of execution order. After consulting the data, I found that the interceptor was created before the springcontext, and the StringRedisTemplate has not been loaded yet, so the injection is directly empty. The core problem is that the interceptor is executed before the automatic bean initialization.

problem solved:

        Inject the interceptor into spring in WebMvcConfigurer, then our interceptor will be created after springcontext and can be successfully injected

    

Supongo que te gusta

Origin blog.csdn.net/Leungiyee/article/details/129614437
Recomendado
Clasificación