引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:Spring 5 中没有符合条件的 bean

钱丹·戈什

我正在尝试使用 Spring 5 编写服务,但是我收到了一个依赖注入问题。我在使用 @Resource 时遇到问题。我最近从 Spring 4 迁移到 Spring 5.0.5,并且在 classapath 中有以下 jars

  • spring-aop-5.0.5.RELEASE
  • spring-beans-5.0.5.RELEASE
  • spring-context-5.0.5.RELEASE
  • spring-core-5.0.5.RELEASE
  • spring-expression-5.0.5.RELEASE
  • spring-test-5.0.5.RELEASE
  • junit-4.12
日志

由于您将类的上下文配置定义为 TestClass.OptionalConfiguration 那么这是您的测试知道的唯一配置。如果您还想在此上下文中将 CiScheduledExecutor 定义为 bean,那么您需要在测试配置中对其进行组件扫描:

@Configuration
@ComponentScan("my.base.package")
static class OptionalConfiguration {
        @Bean
        List<Item> someString() {
            return new ArrayList<>();
        }
        @Bean
        Object foo(List<Item> obj) {
            return new Object();
        }
    }

尽管如果您正在进行集成,例如需要从实际运行时 Spring 上下文中提取多个 bean 定义的测试,那么您应该考虑使用 @SpringBootTest ,它为您完成了很多工作,请参见此处:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章