In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to solve the Consider defining a bean of type 'xxx' in your configuration problem in spring boot automatic injection". In the daily operation, it is believed that many people have doubts about how to solve the Consider defining a bean of type' xxx' in your configuration problem in spring boot automatic injection. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "how to solve the Consider defining a bean of type 'xxx' in your configuration problem in spring boot automatic injection"! Next, please follow the editor to study!
After building the demo of spring boot, it is natural to implement automatic injection to reflect the convenience of spring ioc, but I have such a problem in the process of implementation. See below. Here we find a solution and write it down for reference for friends who encounter the same problem.
Description:Field helloService in com.example.demo.service.TestController required a bean of type 'com.example.service.HelloService' that could not be found.Action:Consider defining a bean of type' com.example.service.HelloService' in your configuration.
Then I looked at some of the classes I wrote and the notes below, and I felt that there was no problem with the writing.
Controller TestController
Interface HelloService
According to the hint in English, a bean of the specified automatic injection type cannot be found in the configuration. After multi-party investigation, a conclusion is drawn:
Normally, a class annotated with @ Component is automatically scanned by Spring to generate Bean to register in the spring container. Since he said that the annotation was not found, that is, the annotation was not recognized by spring, the crux of the problem lies in the annotation SpringBootApplication of the application class.
This annotation is actually equivalent to the effect of the following pile of annotations. One of the annotations is @ Component. By default, you can only scan @ Component annotations under the same package and its subpackages as the controller, and @ Service@Controller and @ Repository that can automatically register the specified annotated classes as Bean. Now I understand the problem. Previously, I put the interface and the corresponding implementation class in the same level directory as the controller package. Such comments are naturally unrecognizable.
@ SpringBootConfiguration@EnableAutoConfiguration@ComponentScan (excludeFilters= {@ Filter (type=CUSTOM, classes= {TypeExcludeFilter.class}), @ Filter (type=CUSTOM, classes= {AutoConfigurationExcludeFilter.class}) @ Target (value= {TYPE}) @ Retention (value=RUNTIME) @ Documented@Inherited
So far, there are two solutions:
1. It is the easiest way to put the interface and the corresponding implementation class in the same directory or its subdirectory as the application startup class, so that the annotations can be scanned.
2. Add such a line to the specified application class to manually specify which package comments the application class will scan, as shown in the following figure
In both ways, the error that could not find the specified Bean was successfully resolved. So that's it for this one.
PS: controller Controller should also be placed in the same level or subdirectory as application. The reason is roughly the same.
At this point, the study on "how to solve the Consider defining a bean of type 'xxx' in your configuration problem of spring boot automatic injection" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.