简体   繁体   English

使用 Spring Boot 2.2+,当应用程序不在云平台上运行时,如何创建 bean

[英]With Spring Boot 2.2+, how can you create a bean when the application is NOT running on a Cloud Platform

In Spring Boot 2.2 there is an annotation @ConditionalOnCloudPlatform which can be used to detect whether the application is running on a Cloud Platform.在 Spring Boot 2.2 中有一个注解@ConditionalOnCloudPlatform可以用来检测应用程序是否在云平台上运行。

I have the reverse issue - I want to not run a bean which is running on a Cloud Platform.我有相反的问题 - 我不想运行在云平台上运行的 bean。 For example, I have code which I don't want to run when running on Kubernetes (as the platform where I run Kubernetes already supplies the functions in the given bean).例如,我有代码在 Kubernetes 上运行时不想运行(因为我运行 Kubernetes 的平台已经提供给定 bean 中的功能)。

What's the best approach to do this?这样做的最佳方法是什么? I feel like I need a @ConditionalOnCloudPlatform annotation.我觉得我需要一个@ConditionalOnCloudPlatform注释。

Thanks.谢谢。

Use @ ConditionalOnMissingBean with the name of Kubernetes bean of @ ConditionalOnMissingClass with one of bean's (or related) classes.@ ConditionalOnMissingBean与@ConditionalOnMissingClass 的名称为@ ConditionalOnMissingClass bean 与bean 的(或相关的)类之一一起使用。

Solution 1解决方案 1

It seems you are using Spring Cloud Kubernetes .看来您正在使用Spring Cloud Kubernetes If so, when an application runs as a pod inside Kubernetes a Spring profile named 'kubernetes' is automatically get activated.如果是这样,当应用程序作为 Kubernetes 内的 pod 运行时,名为“kubernetes”的 Spring 配置文件会自动激活。

@Component
@Profile("!kubernetes")
public class MyLocalTestBean {}

Solution 2解决方案 2

Invert condition using NonNestedConditions (the code below is not verified)使用NonNestedConditions反转条件(下面的代码未经验证)

class ConditionalOnNotInCloud extends NoneNestedConditions {

    ConditionalOnNotInCloud() {
        super(ConfigurationPhase.PARSE_CONFIGURATION);
    }

    @ConditionalOnCloudPlatform
    static class OnCloudCondition {
    }
}

@Bean
@Conditional(ConditionalOnNotInCloud.class)
public StubBean createStubBean() {}

暂无
暂无

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

相关问题 在 STS 中运行 Spring Boot 应用程序时找不到 Bean - Bean could not be found when running Spring Boot application in STS 在Spring引导测试中,如何创建一个bean并通过@Autowired注入? - In a Spring boot test, how can I create a bean and injects by @Autowired? 无法让 Spring 引导应用程序在 Google Cloud Platform Flexible Environment 上工作 - Can't get Spring Boot application to work on Google Cloud Platform Flexible Environment 如何在 Spring 引导应用程序中测试 @Configuration 和 @Bean? - How to test @Configuration and @Bean in a Spring Boot Application? 如果我们已在Google Cloud Platform中部署Spring Boot应用程序,如何查看日志? - How to see the logs if we have deployed our Spring Boot Application in Google Cloud Platform? 运行 spring 时启动应用程序需要一个找不到类型的 bean - when running spring Boot application getting required a bean of type that could not be found Tomcat 通过 Jar 运行 Spring 引导应用程序时不出现(缺少 ServletWebServerFactory bean) - Tomcat Doesn't Come Up When Running Spring Boot Application Through the Jar (missing ServletWebServerFactory bean) 如何在执行测试时阻止我的 Spring Boot Batch 应用程序运行? - How can I prevent my Spring Boot Batch application from running when executing test? 如何在没有Spring Boot的情况下创建Spring Sleuth应用程序 - How can I create a Spring sleuth application without Spring boot 如何在spring boot中使用@Bean为抽象类创建bean - How to create bean using @Bean in spring boot for abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM