diff --git a/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java b/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java index ed3b027..bbdb7e8 100644 --- a/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java +++ b/kenaito-config-demo/src/main/java/cn/odboy/rest/DemoController.java @@ -1,19 +1,43 @@ package cn.odboy.rest; -import lombok.RequiredArgsConstructor; +import java.util.HashMap; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; - +/** + * 全局更新配置测试 + * + * @author odboy + * @date 2024-12-06 + */ @RestController -@RequiredArgsConstructor -@RequestMapping("/api/demo") +@RequestMapping("/updateConfig") public class DemoController { + @Value("${kenaito.config-center.demo:123}") + private String demoStr; - @PostMapping("/test") - public ResponseEntity test() { - return ResponseEntity.ok("success"); - } + @Autowired private ConfigurableEnvironment environment; + + /** 配置变化了 */ + @GetMapping("/test") + public ResponseEntity test() { + String key = "kenaito.config-center.demo"; + String property = environment.getProperty(key); + System.err.println("property=" + property); + Map properties = new HashMap<>(); + properties.put(key, "Hello World!"); + MapPropertySource propertySource = new MapPropertySource("dynamicProperties", properties); + // 将新的属性源添加到 ConfigurableEnvironment 中 + environment.getPropertySources().addFirst(propertySource); + String property1 = environment.getProperty(key); + System.err.println("property1=" + property1); + return ResponseEntity.ok("success"); + } }