2017-05-05 5 views
0

Javaクラスを曇らせる:更新spring.active.profile - 春ブーツと

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を押すと、「Hello mock-api.com!」と応答しています。

application.ymlからmocked-ecomを削除して保存してからポストリフレッシュAPIコールhttp://localhost:8080/refreshを呼び出してコンテキストを更新すると、結果 "Hello dev-api.com!"しかし、私は "こんにちはmock - api.com!"を得ています。

春の起動時にrefreshscopeを使用して実行時にプロファイルをリフレッシュする方法はありますか?

spring: 
    profiles: 
    active: default 

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

management: 
    security: 
    enabled: false 

答えて

0

実行時にSpringプロファイル値を変更できるかどうかはわかりません。アクティブなプロファイルの1つのプロパティの値を変更することができ、/ refreshへのPOSTは新しい値を選択する必要があります。変更してみてください:

アプリケーション嘲笑-ecom.yml応答のための

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

感謝。私はプロパティを変更してみましたが、新しい値が表示されますが、可能であればプロフィールを変更する必要があります – user2057006