首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot和@ComponentScan在两个罐子之间

Spring Boot和@ComponentScan在两个罐子之间
EN

Stack Overflow用户
提问于 2014-03-16 20:49:32
回答 2查看 47.2K关注 0票数 17

我有两个项目。一个是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,但这里仍然没有运气,这是什么样的注释。

摘自评论:

组件扫描声明

代码语言:javascript
复制
@ComponentScan({"com.foo.dal.*","com.foo.notification.*"})

堆栈跟踪:

代码语言:javascript
复制
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

代码语言:javascript
复制
@ComponentScan({"com.teradata.notification","com.teradata.dal"})

跑到:

代码语言:javascript
复制
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项目:

服务项目:

EN

回答 2

Stack Overflow用户

发布于 2014-03-17 09:47:30

@ComponentScan的参数是包名,这些字符串不是有效的包。从它们中删除.*;Spring自动扫描子包。

票数 19
EN

Stack Overflow用户

发布于 2017-04-26 11:51:08

同样的问题有一段时间了,然后@EntityScan为我做了这个把戏,就像在这里建议的那样-- Spring Boot w/ JPA: move @Entity to different package

希望这有帮助

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22442822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档