Original: https://my.oschina.net/u/1790105/blog/1490098
These two days encountered a problem with the bean in the Springboot interceptor that was unable to inject. Here is a description of my thinking process and the resolution process:
1. Because other beans in the Service,controller layer injected a little bit of a problem, and began not to realize that the bean can not inject is invalid in the interceptor problem, has been looking for annotations specified where the package is configured, but can not find the configuration, The Springboot is loaded in the form of a Java class. See this in one corner of the network:
The Bean assembly default rules for Springboot projects are scanned from top to bottom based on the package location where the application class resides!
"Application class" refers to the Springboot project entry class. The location of this class is critical:
If the package for the application class is: Com.boot.app, only the Com.boot.app package and all its child packages will be scanned, and if the service or DAO package is not under Com.boot.app and its child packages, it will not be scanned!
That is, the application class is placed in the DAO, the service is the superior of the package, com.boot.Application
Knowing this is critical, and I don't know if there is any explanation in the spring documentation, and if you don't know it, it can't be solved.
The class I'm having trouble with is really under the application class sub-package, which doesn't seem to be the problem.
2. Start to realize that there is only such a problem on the interceptor, the query reason should be:
Interceptor execution causes this problem before automatic bean initialization.
That's just the problem with the interceptor, and here's the workaround:
The first is my interceptor and the Iredisutil object to inject, and the problem is this iredisutil.
To solve the problem is to add the Interceptor class as a bean in the project that inherits the "Webmvcconfigureradapter" class, as follows:
Now go to run and find that the Iredisutil object has a value.
Springboot the service or Redis injection to NULL in the Interceptor