【发布时间】:2023-03-25 08:01:01
【问题描述】:
Java 类:
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
application-mocked-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!”但我收到“Hello mock-api.com!”。
如何在 Spring Boot 中使用 refreshscope 在运行时刷新配置文件?
spring:
profiles:
active: default
integrations:
ecom:
api-url: dev-api.com
management:
security:
enabled: false
【问题讨论】:
标签: spring spring-boot spring-cloud spring-cloud-config