Spring not recognising some @Configuration and @Component classes
I have - or rather had - a working Spring application, running within IntelliJ. It contains several classes annotated with @Configuration
, and several @Component
beans. At some point, the following happened:
Intelli started showing errors in the code editor stating "Could not autowire. No bean of 'xxx' type found". But there are such beans which are annotated with @Component
.
Breakpoints in the constructor of specific @Component
beans are not reached. But that is not true for all @Component
beans.
When running in debug mode, breakpoints in certain @Configuration
files are not reached, even though the debugger was stopping there before. The application will fail if it is autowired with one of these @Component
beans.
The application starts without errors, but obviously without several beans configured in @Configuration
classes being called.
The class which contains the main method which runs the Spring Boot application is annotated with @SpringBootApplication
. @Component
classes which live in the same package as this class are recognised and can be autowired, even into classes in other packages.
I am not aware of anything in the code or project which would have changed.
Under File -> Project Settings -> Modules , under Spring Application Context have now selected all @Configuration
files. However this makes no difference.
Have also tried Build -> Rebuild Project .
The packages in which the classes reside have not changed. Has anyone seen anything like this before?
Thanks
If few classes are not getting recognised @Component. Then it could be the case that those classes don't come under the same package. You must have observed that the classes under the same package as of Main class of @SpringBootApplication, got recognised with @Component because @SpringBootApplication defines an automatic @ComponentScan on the package.
So other classes which were defined in some other package are not recognised because there is no @ComponentScan for those classes' package.
You can do the following to get those classes recognised(add the other packages which are not directly under the hierarchy of @SpringBootApplication):
@ComponentScan({"com.example.springboot.anything","com.example.springboot.somethingelse"})
@SpringBootApplication
public class AnySpringBootApplication {
链接地址: http://www.djcxy.com/p/67732.html