【问题标题】:Spring Boot and @ComponentScan between two jars两个 jar 之间的 Spring Boot 和 @ComponentScan
【发布时间】:2014-04-21 23:26:40
【问题描述】:

我有 2 个项目。 一个是 DAL 项目,它使用 spring neo4j APIs 在 neo4j DB 上执行 CRUD 操作。该项目被打包为 jar 并包含在项目 #2 中。 项目 #2 是一个 Spring RESTful 服务项目,它使用 Spring Boot 打包并创建一个在嵌入式 tomcat 服务器上运行的可执行 jar。

当尝试运行 Spring boot 为我创建的可执行 jar 时,我不断收到此异常。 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

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

从 cmets 中提取:

组件扫描声明

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






服务项目:




【问题讨论】:

  • 抱歉忘记了我正在使用的注释
  • @ComponentScan({"com.foo.dal.*","com.foo.notification.*"})
  • 不要使用“谢谢”,请附上代码示例。以及抛出的异常。
  • 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
  • 更多细节是好的,但总是像你的图像内容一样发布文本作为文本。图片难以阅读,无法搜索或复制以进行测试。

标签: java spring spring-mvc spring-boot


【解决方案1】:

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

【讨论】:

  • 你是对的,但这无济于事。正如@sinisa229 所说,缺少的自动装配依赖项不在扫描的包中,即使名称是固定的......
  • @DirkLachowski 看起来确实如此; OP 发布了混淆的包名称,但并没有完全做到这一点,我也想这么多。
  • Dirk Lachowski 我以 @ComponenetScan 中的包名 foo 为例,忘记在堆栈跟踪中更改它。
  • @MuhiMasoud 所以基本上组件扫描现在正在工作,但是您正在运行另一个错误(由弹簧启动触发)。看来你得问另一个问题了。
  • 你应该接受这个问题的答案(否则你的名声只会越来越差)。
【解决方案2】:

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

希望有帮助

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 2014-11-17
    • 2018-06-14
    • 2017-07-25
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多