【问题标题】:How do I tell Spring to skip loading a Configuration class based on a property?如何告诉 Spring 跳过加载基于属性的配置类?
【发布时间】:2018-09-24 07:56:49
【问题描述】:

我知道通过@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
}

这样的事情可能吗?

【问题讨论】:

标签: java spring


【解决方案1】:

查看您自己条件的实现或使用@ConditionalOnProperty() 类似[this question] 的答案建议。 (Conditional spring bean creation)

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 2020-04-28
    • 2020-05-23
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2022-01-02
    相关资源
    最近更新 更多