Solution to the problem of consider defining a bean of type ‘xxx’ in your configuration in spring boot auto injection

Website content quality is poor, distribution efficiency is too low how to do?Huawei engineers offer 5 unique skills>>>

According to the English prompt, we can’t find a bean with the specified auto injection type in the configuration. After a lot of investigation, we come to the conclusion:
under normal circumstances, the class with @ Component annotation will be automatically scanned by spring to the generated bean and registered in the spring container. Since he said that it can’t be found, that is, the annotation is 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 batch of annotations, one of which is @ component. By default, it can only scan @ component annotations under the same package as the controller and its subpackages, And can automatically register the specified annotation class as a [email protected] [email protected] Before I put the interface and the corresponding implementation class in the same level directory as the package where the controller is located, such annotations are naturally unrecognized
and @ repository

@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, Two solutions are obtained:
1. Put the interface and the corresponding implementation class in the same directory or its subdirectory as the application startup class, so that the annotation can be scanned, which is the most convenient way
2. Add such a line of annotation to the specified application class, and manually specify which package annotations the application class should scan, See the following figure

through these two ways, the error that the specified bean cannot be found is successfully solved….. So that’s it
PS: the controller should also be placed in the same level or subdirectory as the application, and the reason is roughly the same
PS: the controller should be placed in the same level or subdirectory as the application

Similar Posts: