2017-05-05 77 views
0

的Java类:刷新spring.active.profile refreshscope - 春天引导和云

package com.hm.refreshscoperesearch; 

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.context.config.annotation.RefreshScope; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

@SpringBootApplication 
@RefreshScope 
@RestController 
public class RefreshScopeResearchApplication { 

    @Value("${integrations.ecom.api-url}") 
    private String url; 

    @RequestMapping("/hello") 
     String hello() { 
      return "Hello " + url + "!"; 
     } 

    public static void main(String[] args) { 
     SpringApplication.run(RefreshScopeResearchApplication.class, args); 
    } 
} 

application.yml

spring: 
    profiles: 
    active: default,mocked-ecom 

integrations: 
    ecom: 
    api-url: dev-api.com 

management: 
    security: 
    enabled: false 

应用嘲弄,ecom.yml

integrations: 
    ecom: 
    api-url: mock-api.com 

当我打,http://localhost:8080/hello,即时通讯获得响应“你好mock-api.com!”。

如果我从application.yml中删除mocked-ecom并保存它,然后调用post refresh api调用http://localhost:8080/refresh来刷新上下文,我期待结果“Hello dev-api.com!”但我得到“你好mock-api.com!”。

如何在运行时使用refreshscope在spring启动时刷新配置文件?

spring: 
    profiles: 
    active: default 

integrations: 
    ecom: 
    api-url: dev-api.com 

management: 
    security: 
    enabled: false 

回答

0

我不知道,你可以在运行时更改Spring配置文件值。您可以更改其中一个活动配置文件中的属性值,并且发布到/ refresh应该选择新的值。尝试改变:

应用嘲弄,ecom.yml

integrations: 
    ecom: 
    api-url: new-mock-api.com 
+0

感谢您的答复。我试图通过改变属性和新的价值出现,但我有要求改变配置文件,如果可能的话 – user2057006