springcloud~nacos通過@refreshScope進行配置熱更新

  • 配置類
@Data
@ConfigurationProperties("auth")
public class AuthProperties {
    private String title;
}

  • 配置類的註冊,需要聲明@RefreshScope
@Configuration
public class AuthConfig {
    @Bean
    @RefreshScope
    public AuthProperties authProperties(){
        return new AuthProperties();
    }
}
  • 代碼中直接注入它
@Autowired
public AuthProperties authProperties;

需要註冊,我們需要把熱更新的配置寫到類中,通過bean去註冊它,直接使用@Value在代碼中注入,是不能實現熱更新的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章