Spring Boot和@ComponentScan在两个JAR之间

17

我有两个项目。 一个是DAL项目,使用Spring Neo4j API对Neo4j数据库执行CRUD操作。这个项目被打包成jar并包含在项目#2中。 项目#2是一个Spring RESTful服务项目,使用Spring Boot将其打包成可执行的jar文件,该文件在嵌入式Tomcat服务器上运行。

当我尝试运行Spring Boot为我创建的可执行jar时,我总是会得到这个异常。 预期至少有1个bean符合此依赖项的自动装配候选人。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

根据我的阅读,如果我使用@ComponentScan,我可以给注释指定要查找的目录。所以我给了它我的服务项目的基本目录。我还给了它我的包含的DAL.jar的基本目录,但仍然没有运气。以下是注释的样子。

从注释中提取的Component Scan声明:

@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答案进行修改: 对@ComponentScan进行更改

@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项目:

DAL project structure


DAL classes


DAL classes


DAL classes


DAL classes


服务项目:

Service project structure


Service classes


Service classes


Service classes


抱歉,我忘记注明我正在使用的注释。 - Muhi Masoud
@ComponentScan({"com.foo.dal.","com.foo.notification."}) - Muhi Masoud
请勿使用“谢谢”,并附上代码示例。同时附带抛出的异常。 - Paweł Dyda
1
预期至少有一个符合自动装配候选的bean来满足此依赖项。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required = true)}。 - Muhi Masoud
2
更多的细节是好的,但是请将像图像内容这样的文本作为文本发布。图像很难阅读,也不能搜索或复制进行测试。 - chrylis -cautiouslyoptimistic-
显示剩余4条评论
2个回答

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

你是对的,但那并没有什么帮助。正如 @sinisa229 所说,缺少的自动装配依赖项不在扫描的包中,即使名称已经修复也无济于事... - Dirk Lachowski
Dirk Lachowski我在@ComponenetScan中使用了包名foo作为示例,忘记在堆栈跟踪中更改它。 - Muhi Masoud
1
@MuhiMasoud,基本上组件扫描现在已经可以工作了,但是你遇到了另一个错误(由 Spring Boot 触发)。看起来你需要问另一个问题。 - Dirk Lachowski
@DirkLachowski 我打算先在这个问题上添加更多细节,然后再提一个新问题。只是因为我没有10分所以无法发布屏幕截图。 - Muhi Masoud
4
你应该接受这个问题的答案(否则你的声誉只会变得更糟)。 - Dave Syer
显示剩余4条评论

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接