【问题标题】:Autowiring clock across projects causes 'No Such Bean found' exception跨项目自动装配时钟导致“未找到此类 Bean”异常
【发布时间】:2023-04-08 11:58:01
【问题描述】:

我的项目设置如下。

如图所示,我有一个辅助项目(带有 2 个 @Autowired 时钟),而我的主项目带有 1 个 @Autowired 时钟。所有三个时钟都表示同一个对象。

当我启动应用程序时,我收到错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“helper2”的 bean 时出错:不满足的依赖关系 通过字段“时钟”表示;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 可用的“java.time.Clock”类型的合格bean:至少预期 1 个有资格作为自动装配候选者的 bean。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)

.

.

.

.

~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] 原因: org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 可用的“java.time.Clock”类型的合格bean:至少预期 1 个有资格作为自动装配候选者的 bean。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

【问题讨论】:

  • 你如何提供你的bean定义?通过 .xml 文件?如我所见,您有MainConfig.class,其中定义了@Bean,但在这种情况下,该类需要标记为@Configuration。
  • 在主配置中,您将时钟类型的 bean 和自动装配定义为类成员。为什么你需要@autowired 时钟?
  • 你有一个循环依赖。
  • 谢谢 Devdio。我没有使用 xml 文件。我用 @Configuration 标记了我的 MainConfig,但我得到了同样的错误。
  • 感谢您的回复 Himanshu。我已经从 MainConfig 中删除了 Autowired。 MainConfig 只有方法,@Bean public Clock getClock() { return Clock.systemDefaultZone(); Helper2 和 Helper3 具有 Autowired Clock 时钟注释。仍然是同样的错误。

标签: java spring spring-boot exception autowired


【解决方案1】:

您不能自动装配 java.time.Clock,因为它不是 Spring 组件。弹簧组件是考虑进行自动检测的类。

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Component.html

我正在编辑我的回复,但似乎@ptikobj 已经很好地回答了这个问题。我要补充一点,将@SpringBootConfiguration 添加到类中而不是仅添加@Configuration 将已经处理组件扫描,因此不需要这样做。

【讨论】:

  • 这个答案似乎不够全面,如果您愿意发布答案,请详细说明以帮助OP。
【解决方案2】:

Spring 需要一个实现 java.time.Clock 的 Bean 定义,以便能够自动装配它。由于您已经将Clock.systemDefaultZone 的返回值公开为@Bean,您唯一需要做的就是将@Configuration 添加到您的MainConfig.java 并确保MainConfig 被Spring 正确拾取,或者通过@ 987654327@或@Import。

【讨论】:

  • 谢谢。我已将“配置”注释添加到 MainConfig。我仍然得到同样的错误。需要注意的是,“找不到Bean”异常出现在导入到主项目中的依赖项目的Helper2类中,而不是在MainConfig类中。我将无法将 MainConfig 类“导入”或“ComponentScan”到依赖项项目中,因为它将创建一个循环依赖项。有没有其他方法可以修复代码?
  • 不需要循环编译时依赖——假设你的@SpringBootApplication也在你的“主Spring Boot应用程序”中。只需将@Import(MainConfig.class) 添加到带有@SpringBootApplication 注释的类中即可。
  • 谢谢 ptikobj,我需要 Helper2 和 Helper3 类中的“时钟”。我应该删除这两个类中的@Autowired Clock 声明吗?那么我将如何从 MainConfig 访问时钟?
  • 无需从Helper2 和Helper3 中删除@Autowired Clock。 Spring 的 Bean Container 是一个运行时概念,因此不关心 Helper2 和 Helper3 是否具有对 MainConfig 的编译时访问权限。您必须确保 Helper1、Helper2、Helper3 和 MainConfig 在相同的 Spring 应用程序上下文中,如果您没有更改任何 Spring Boot 的默认值。
猜你喜欢
  • 1970-01-01
  • 2012-05-16
  • 2015-11-16
  • 2017-02-17
  • 2014-11-17
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
相关资源
最近更新 更多