我有两个项目。一个是DAL项目,它使用spring neo4j API对一个neo4j DB执行CRUD操作。该项目被打包为jar,并包含在项目2中。项目2是一个Spring服务项目,它使用spring引导来打包和创建一个可执行的jar,该jar在嵌入式tomcat服务器上运行。
在尝试运行Spring引导为我创建的可执行jar时,我一直得到这个异常。预期至少有一个bean可以作为此依赖项的自动测试候选。依赖性注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
根据我的阅读,如果我使用@ComponentScan,我可以给出要查看的注释目录。所以我给了它我的服务项目的基本数据。我给它包含的DAL.jar的基本dir,但这里仍然没有运气,这是什么样的注释。
摘自评论:
组件扫描声明
@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项目:





服务项目:




发布于 2014-03-17 09:47:30
@ComponentScan的参数是包名,这些字符串不是有效的包。从它们中删除.*;Spring自动扫描子包。
发布于 2017-04-26 11:51:08
同样的问题有一段时间了,然后@EntityScan为我做了这个把戏,就像在这里建议的那样-- Spring Boot w/ JPA: move @Entity to different package。
希望这有帮助
https://stackoverflow.com/questions/22442822
复制相似问题