简体   繁体   中英

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type in Spring 5

I am trying to write a service using Spring 5, however I receive a Dependency Injection issue. I'm having problems using @Resource.I have recently migrated from Spring 4 to Spring 5.0.5 and have the following jars in the classapath

  • 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

Since you are defining your context configuration for your class as TestClass.OptionalConfiguration then this is the only configuration your tests know about. If you want to define CiScheduledExecutor as a bean in this context as well then you need to component scan for it in your test configuration:

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

Although if you are doing integration like tests that require pulling in several bean definitions from your actual runtime Spring context then you should consider using @SpringBootTest which does a lot of the work for you, see here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM