我正在使用 Spring Cloud 配置,我需要在运行时将 spring.cloud.config.profile
属性重写为 blue
或 green
我有一个方法 - defineConfigProfile()
,它返回 blue
或 green
,但是如何告诉 Spring Boot 在获取配置(即解析配置属性)之前运行此方法以及如何告诉 Spring Boot 覆盖系统属性?
我在这里看到https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html "profile"= ${spring.profiles.active} (实际上是 Environment.getActiveProfiles())
但我不知道如何设置 Activity 配置文件。
最佳答案
留下这个是因为我认为它很有用,我所做的是在我的公共(public)类Application extends SpringBootServletInitializer
中:
我做了以下事情:
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
String profile = determineConfigProfile();
System.setProperty("spring.cloud.config.profile", profile);
return application.sources(Application.class);
}
这会在 Spring 云配置启动之前运行,并覆盖配置文件。
关于java - 在运行时覆盖 `spring.cloud.config.profile`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169571/