我有2个项目.一个是DAL项目,它使用spring neo4j API对neo4j DB进行CRUD操作.该项目打包为jar并包含在项目#2中.Project#2是一个Spring restful服务项目,它使用spring boot打包并创建一个可嵌入tomcat服务器上的可执行jar.
当我试图运行Spring引导为我创建的可执行jar时,我一直得到这个异常.预计至少有1个bean可以作为此依赖项的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
根据我的阅读,如果我使用@ComponentScan,我可以给注释目录查看.所以我给它我的服务项目的基础目录.我给它包含了DAL.jar的基础目录,但是这里的注释看起来仍然没有运气.
摘自评论:
组件扫描声明
@ComponentScan({"com.foo.dal.*","com.foo.notification.*"})
堆栈跟踪:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pushCommandsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.teradata.dal.example.PushRepository com.teradata.notification.rest.controller.PushCommandsController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.teradata.dal.example.PushRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
更新:
基于@chrylis回答:改为@ComponenetScan
@ComponentScan({"com.teradata.notification","com.teradata.dal"})
跑进去:
java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) at java.lang.Thread.run(Thread.java:744) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration' is defined
关于DAL项目和服务项目的更多细节:
DAL项目:
服务项目:
参数to @ComponentScan
是包名称,并且这些字符串不是有效的包..*
从他们身上掉下来; Spring会自动扫描子包.