简体   繁体   English

spring boot boostrap bean override

[英]spring boot boostrap bean override

In my spring.factories I have such a definition:在我的spring.factories我有这样一个定义:

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
     MyBootstrapConfiguration

In MyBootstrapConfiguration I have:MyBootstrapConfiguration我有:

@Configuration
public class MyBootstrapConfiguration {
    
     @Bean
     @ConditionalOnMissingBean
     public ApiClient apiClient() {
         return someClient;
     }
}

Now in my test ( junit-5 and @SpringBootTest ), I would like to override this bean.现在在我的测试中( junit-5@SpringBootTest ),我想覆盖这个 bean。 Notice the @ConditionalOnMissingBean ... If I can hook somehow to a "before" my bootstrap is started, to provide that ApiClient , that method apiClient would obviously not be called and I would be happy.注意@ConditionalOnMissingBean ...如果我能以某种方式挂钩到我的引导程序启动的“之前”,以提供ApiClient ,显然不会调用该方法apiClient ,我会很高兴。

Please notice that MyBootstrapConfiguration is not something I have control of - its an invariant to me.请注意MyBootstrapConfiguration不是我可以控制的东西——它对我来说是不变的。 For non-bootstrap configuration this is easily doable, but is there a way to do this for bootstrap?对于非引导程序配置,这很容易做到,但是有没有办法为引导程序执行此操作?

Given a test configuration class:给定测试配置 class:

@Configuration
public class TestBootstrapConfiguration implements Ordered {
    
    @Bean
    public ApiClient testApiClient() {
        return testApiClient;
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}

In src/test/resources/META-INF/spring.factoriessrc/test/resources/META-INF/spring.factories

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
TestBootstrapConfiguration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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