目标:修改nacos配置文件,在不重启程序的情况下实现字段值的变更。
方式一
使用@RefreshScope注解写在类上,该类中使用@Value注解的变量会随nacos配置文件中的值变化而即时变化。
@Controller
@RefreshScope
public class Controller {
@Value("${user.age}")
private int age;
@Value("${user.name}")
private String name;
......
}
方式二
使用@ConfigurationProperties注解标注配置类,在通过Nacos发布配置后,配置类会被重新rebind,此时变量动态修改生效,使用的地方通过@Autowired注入。
@Data
//@Configuration或者@Component
@Configuration
@ConfigurationProperties("user")
public class User {
private int age;
private String name;
}
本文介绍如何利用Nacos实现在不重启程序的情况下动态修改配置文件字段值。主要通过两种方式实现:一是使用@RefreshScope注解配合@Value注解;二是采用@ConfigurationProperties注解配置类并结合@Autowired注入。
828

被折叠的 条评论
为什么被折叠?



