spring-cloud-commons: Problem with `RefreshScope`, centralized configuration and type safe configuration
Spring Boot 2.0.0.RC1 - Spring Cloud Finchley.M6 - JDK8
Hello,
Consider 2 SpringBoot applications (config server + app) as defined in https://spring.io/guides/gs/centralized-configuration/
When you map configuration to a bean with @ConfigurationProperties and you try to change it runtime here is the behaviour I found:
First time you update the property in the config server it works
Second time you update the property it works for properties injected with @Value but it doesn’t work for those injected as @ConfigurationProperties.
You can find a sample project in https://github.com/bleporini/refresh .
@RestController
@RefreshScope
@Slf4j
public class MyController {
private final Features features;
private final String test;
public MyController(Features features,
@Value("${features.test}") String test) {
this.features = features;
this.test = test;
}
@Scheduled(fixedDelay = 1000)
public void trace(){
log.error("{} / {}", features.getTest(), test);
}
@GetMapping("/o")
public String hello(){
return features.getTest();
}
@ConfigurationProperties("features")
@Slf4j
public static class Features {
@Getter
private String test;
public void setTest(String test) {
log.warn("{} Was/ will be : {} / {}", identityHashCode(this), this.test, test);
this.test = test;
}
}
}
It starts with a value set to Hello1:
2018-02-20 18:45:18.056 ERROR 58210 --- [pool-2-thread-1] com.example.refresh.MyController : Hello1 / Hello1
Fine
Now we set the value to Hello12 and it still works fine, except now the bean is lazily initialized (that’s why there’s a @GetMapping method in it but it is another story), so after waking up the bean with a curl:
2018-02-20 18:45:46.358 ERROR 58210 --- [pool-2-thread-1] com.example.refresh.MyController : Hello12 / Hello12
Second time we update back the value to Hello1, the @Value is updated but not the bean, but now the bean is not lazily initialized, so no need to curl the app :
2018-02-20 18:45:58.394 ERROR 58210 --- [pool-2-thread-1] com.example.refresh.MyController : Hello12 / Hello1
About the lazy initialisation after the first update, I think this is also an issue because in the sample there’s a @Scheduled method, so after the first update, the bean seems to be not bound in the application context, and thus the scheduled method is not called till the bean is bound.
Note that the problem with the configuration properties disappears if you downgrade to Spring Boot 1.5.10 and Spring Cloud Edgware.SR2.
Note that the problem with the laziness is still the same with Spring Boot 1.5.10 and Spring Cloud Edgware.SR2.
Regards
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 45 (21 by maintainers)
This issue will be fixed by Spring Boot 2.0.1 due Mar 29. That should be what Finchley.RC1 builds upon.