java - 在运行时覆盖 `spring.cloud.config.profile`

标签 java spring spring-boot spring-cloud-config

我正在使用 Spring Cloud 配置,我需要在运行时将 spring.cloud.config.profile 属性重写为 bluegreen

我有一个方法 - defineConfigProfile(),它返回 bluegreen,但是如何告诉 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/

相关文章:

java - 如何按添加位置从数据存储中获取条目? (Google 应用引擎和 JDO)

java - 短路 JavaOptional.flatMap()

java - 在 Transport.send(message) 上获取 ParseException

java - Servlet Spec 3.0 不渲染 JSP 包含

java - Spring 集成: Exception Handling on Publish/Subscribe Channels with Ordered Subscribers

spring - SpringBoot中无法解析ServletRegistrationBean

Java - 根据用户的回答询问问题 x 次

spring - 启用身份验证时如何配置spring boot admin客户端?

java - spring-boot RabbitListener 如何将单个队列绑定(bind)到单个线程?

java - Spring Boot YARN如何部署到Hadoop