I do some initialization in Spring singleton bean @PostConstruct method (simplified code):
@Component
public class Env {
public String currentEnv;
@PostConstruct
public void init() {
currentEnv = "dev";
}
}
Should I worry about currentEnv visibility to other beans(other thread) and mark it volatile.
No, I think it is not necessity.
Only one bean is responsible to initialize the variables and registered it in spring context. So the visibility is guaranteed.
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-postconstruct-and-predestroy-annotations
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.html