两个罐子之间的Spring Boot和@ComponentScan

时间:2014-03-16 20:49:32

标签: java spring spring-mvc spring-boot

我有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 PROJECT:

DAL project structure


DAL classes


DAL classes


DAL classes


DAL classes


服务项目:

Service project structure


Service classes


Service classes


Service classes

2 个答案:

答案 0 :(得分:19)

@ComponentScan的参数是包名,而这些字符串不是有效的包。从他们身上放下.*; Spring会自动扫描子包。

答案 1 :(得分:1)

在短时间内遇到同样的问题,然后@EntityScan为我做了诀窍,就像这里所说的那样 - Spring Boot w/ JPA: move @Entity to different package

希望有所帮助