在使用Nacos作为配置中心时,我们希望能够在更改配置文件之后,可以同步到各个服务中,下面我们介绍一下2种实现方式。
配置文件:
test:
name:
stone
如果使用@ConfigurationProperties+@Configuration
,不需要加@RefreshScope
,也可以实现配置自动刷新
@Configuration
@ConfigurationProperties(prefix = "test")
public class CustomProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果使用@Value
,需要加@RefreshScope
,才能实现配置自动刷新
@RefreshScope
@Component
public class CustomProperties {
@Value("${test.name}")
private String name;
}