Recently, using Springboot's Custom Interceptor, a DAO was injected into the interceptor, ready to do the following, the Interceptor code:
Public class Implements handlerinterceptor { @Autowired private Itokendao Tokendao; @Override Publicbooleanthrows Exception { } ...}
Configuration Information Code:
@Configuration Public classInterceptorconfigextendsWebmvcconfigureradapter {/** * * @paramRegistry*/@Override Public voidaddinterceptors (Interceptorregistry registry) {Registry.addinterceptor (NewTokeninterceptor ()). Excludepathpatterns ("/user/login"); Super. Addinterceptors (registry); }}
There seems to be no problem, but the run result finds that the DAO injected in the token interceptor is null.
Reason
Null is caused by the fact that the interceptor loading was done before Springcontext was created, so injecting the entity into the interceptor is naturally null.
Solve
The workaround is to have the bean load in advance and modify the configuration information to read as follows:
@Configuration Public classInterceptorconfigextendsWebmvcconfigureradapter {@Bean Publichandlerinterceptor Gettokeninterceptor () {return NewTokeninterceptor (); } /** * * @paramRegistry*/@Override Public voidaddinterceptors (Interceptorregistry registry) {Registry.addinterceptor (Gettokeninterceptor ()) . Excludepathpatterns ("/user/login"); Super. Addinterceptors (registry); }}
Re-running DAO can inject success.
The "Springboot" Interceptor uses the @autowired injection interface as a null workaround