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在代码中注入,是不能实现热更新的。

posted @ 2022-09-21 15:36  张占岭  阅读(321)  评论(0编辑  收藏  举报