Springboot the service or Redis injection to NULL in the Interceptor

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.