Spring boot 2.1 bean override vs. Primary

后端 未结 4 869
名媛妹妹
名媛妹妹 2020-12-28 14:04

With Spring Boot 2.1 bean overriding is disabled by default, which is a good thing.

However I do have some tests where I replace beans with mocked instances using Mo

4条回答
  •  心在旅途
    2020-12-28 14:33

    It is allowed to override @Component with @Bean by default. In your case

    @Service
    public class AService {
    }
    
    @Component
    public class BService {
        @Autowired
        public BService() { ... }
    }
    
    @Configuration
    @ComponentScan
    public BaseConfiguration {
    }
    
    @Configuration
    // WARNING! Doesn't work with @SpringBootTest annotation
    @Import({BaseConfiguration.class})
    public class TestConfiguration {
        @Bean // you allowed to override @Component with @Bean.
        public BService bService() {
            return Mockito.mock(BService.class);
        }
    }
    

提交回复
热议问题