[英]How can I force SpringBoot to use One and only One of multiple config classes?
我会在两个配置上使用 @ConditonalOnProperty 注释,并在其中一个运行时的主要中添加属性,以 lambda 为例(因为你说每次运行使用不同的)
public static void main(String[] args) {
SpringApplication application = new SpringApplication(DemoApplication.class);
Properties properties = new Properties();
properties.put("lambda.application", "true");
application.setDefaultProperties(properties);
application.run(args);
}
然后在运行时为 lambda 时所需的配置上,您可以这样注释 bean
@ConditionalOnProperty(
value="lambda.application",
havingValue = "true")
public class DyanmoConfig {
那么另一个 bean 可以具有以下条件属性
@ConditionalOnProperty(
value="lambda.application",
havingValue = "false",
matchIfMissing= true)
public class PersistConfig {
这样你只需要在两个主要的之一中以编程方式设置属性
有不同的方法来解决这个问题。 您选择哪一个取决于您的特定用例和需求。
使用配置文件: https : //www.baeldung.com/spring-profiles
例子:
@Component
@Profile("runtime1")
public class DynamoConfig
使用条件 bean(多种可能性): https : //reflectoring.io/spring-boot-conditionals/
@Component
@ConditionalOnProperty(
value="module.enabled",
havingValue = "true",
matchIfMissing = true)
public class DynamoConfig
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.