Modify active profiles and refresh ApplicationContext runtime in a Spring Boot application

后端 未结 1 738
执笔经年
执笔经年 2020-12-19 05:28

I have a Spring boot Web application. The application is configured via java classes using the @Configurable annotation. I have introduced two profiles: \'i

相关标签:
1条回答
  • 2020-12-19 06:02

    You can't just refresh an existing context. You have to close the old one and create a new one. You can see how we do it in Spring Cloud here: https://github.com/spring-cloud/spring-cloud-commons/blob/master/spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java. If you want to you can include that Endpoint just by adding spring-cloud-context as a dependency, or you can copy the code I guess and use it in your own "endpoint".

    Here's the endpoint implementation (some details missing in fields):

    @ManagedOperation
    public synchronized ConfigurableApplicationContext restart() {
      if (this.context != null) {
        if (this.integrationShutdown != null) {
          this.integrationShutdown.stop(this.timeout);
        }
        this.application.setEnvironment(this.context.getEnvironment());
        this.context.close();
        overrideClassLoaderForRestart();
        this.context = this.application.run(this.args);
      }
      return this.context;
    }
    
    0 讨论(0)
提交回复
热议问题