1、bootstrap.yml中的配置屬性 無法動態刷新
只能添加在application.yml中
2、需要在調用的地方添加@RefreshScope、不能只在Application上添加
3 、需要引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4、在線程中引用無效、在線程while循環 該變量的值一直不變
@Value("${coffee.prop.name}")
private String name;
@Override
public void afterPropertiesSet() throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name);
}
}
}).start();
}
正確的用法
@RestController
@RefreshScope
public class RefreshController {
@Value("${coffee.prop.name}")
private String name;
@GetMapping("getName")
public String getName() {
return name;
}
}