【发布时间】: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
}
这样的事情可能吗?
【问题讨论】:
-
看看
@ConditionalOnProperty:docs.spring.io/autorepo/docs/spring-boot/current/api/org/… -
太完美了!
ConditionalOnProperty正是我要找的东西