This project is a Maven project based on the SpringBoot framework.
Today, when handling interceptor logic in an interceptor, you need to call other methods using annotations and read parameters from the configuration file. So I used the following annotation:
@Reference CoreRedisService redisService; @Value("${channel}") private String channel; @Value("${allowMethod}") private String allowMethod; One is to get the reference to the interface, two other two are to get the parameters in the configuration file.
However, during the debugging process, I found that none of the three were injected and the situation shown in the figure below occurred:
You can see that all three values are null.
I then looked at the configuration of my project to determine if the interceptor's position is within the scope of the annotation. I found no problem. I found a problem on Baidu and found a problem: the time when the interceptor loads is before springcontext, so it is naturally null injected into the interceptor.
According to the solution, the interceptor is first injected into the class that configures the interceptor chain. The code is as follows:
package com.***;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** *ConfigurationInterceptorchain* Created by yefuliang on 2017/10/23. */@Configurationpublic class bgqWebAppConfigurer extends WebMvcConfigurerAdapter { @Bean public bgqCommonInterceptorl bgqCommonInterceptorl() { return new bgqCommonInterceptorl(); } public void addInterceptors(InterceptorRegistry registry) { // Multiple interceptors form an interceptor chain// addPathPatterns is used to add interceptor rules// excludePathPatterns User exclusion intercept registry.addInterceptor(bgqCommonInterceptorl()).addPathPatterns("/**"); super.addInterceptors(registry); }} Note that the injected is the interceptor class, not the class you want to inject into the interceptor, and then the interceptor chain's registry.addInterceptor(bgqCommonInterceptorl()).addPathPatterns("/**");
You don't need to re-new the first parameter in it.
After the modification is corrected, debug:
As you can see, all injected into it and the problem is solved.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.