Springboot 動態刷新的幾點注意

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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章