Java 如何告诉Spring跳过基于属性加载配置类?

Java 如何告诉Spring跳过基于属性加载配置类?,java,spring,Java,Spring,我知道通过@Profile注释,您可以告诉Spring在使用指定的概要文件时只加载某个类,如下所示: @Configuration @Profile("dev") public class MyCustomConfigurationClass { // this will only be instantiated when the "dev" profile is active } 但是,我想知道对于给定的应用程序属性/环境变量,是否有一些等效的方法可以做到这一点?下面是一些伪代码来说

我知道通过@Profile注释,您可以告诉Spring在使用指定的概要文件时只加载某个类,如下所示:

@Configuration
@Profile("dev")
public class MyCustomConfigurationClass {
    // this will only be instantiated when the "dev" profile is active
}
但是,我想知道对于给定的应用程序属性/环境变量,是否有一些等效的方法可以做到这一点?下面是一些伪代码来说明我要做的事情:

@Configuration
@OnlyInstantiateWhen(property = "${my.property}", value = "true")
public class MyCustomConfigurationClass {
    // this would theoretically only be instantiated when
    // the value of my.property is true, either in application.properties
    // or in an environment variable
}

这样做可能吗?

看看您自己条件的实现,或者使用@ConditionalOnProperty类似于[这个问题]的答案

看看您自己条件的实现,或者使用@ConditionalOnProperty类似于[这个问题]的答案

看看@ConditionalOnProperty:太好了!ConditionalOnProperty正是我想要的看看@ConditionalOnProperty:太完美了!条件不动产正是我想要的