需要刷新的对象单独写在一个类中
@Component
public class NacosTestRunner implements ApplicationRunner {
@Resource
private TestProp testProp;
// @Value("${test.a:2}")
// int a;
@Override
public void run(ApplicationArguments args) throws Exception {
CompletableFuture.runAsync(() -> {
while (true) {
try {
System.out.println("a = " + testProp.getA());
if (testProp.getA() == -1) {
break;
}
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
}
@Component
@RefreshScope
public class TestProp {
@Value("${test.a:2}")
private int a;
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
}