I have an instance of a Spring ApplicationContext in a method and I need to "probe" it and decide if:
- it has been fully
refresh()ed at least once so I can be sure that the beanFactory is done creating all of its beans and - no subsequent
refresh()is currently in progress
Important restriction: In my use case I cannot use a listener for ContextRefreshedEvent. I can only operate on the applicationContext that was passed to me, which might have happened during a refresh(), before or after it.
If you take a look at org.springframework.context.support.AbstractApplicationContext#refresh you will see the following steps, among others:
public void refresh() throws BeansException, IllegalStateException {
// :
prepareRefresh(); // makes this.isActive() return true
// :
finishBeanFactoryInitialization(beanFactory);
// internally calls:
// beanFactory.freezeConfiguration() -> makes beanFactoroy.isConfigurationFrozen() return true
// beanFactory.preInstantiateSingletons()
// :
finishRefresh();
}
The above selection of steps highlights the fact that ConfigurableApplicationContext.isActive() and ConfigurableListableBeanFactory.isConfigurationFrozen() which I have tried do not guarantee that beanFactory.preInstantiateSingletons() has been completed, which is very important in my case.
Ideally I would expect
- A
ConfigurableApplicationContext.isRefreshed()method which would indicate if a context has been fully refreshed at least once and - A
ConfigurableApplicationContext.isBeingRefreshed()which would indicate whether a refresh, the first or a subsequent one, is currently in progress.
Schematically they would fit in the existing refresh() method as follows:
public void refresh() throws BeansException, IllegalStateException {
// Addition 1
setIsBeingRefreshed(true)
// :
prepareRefresh();
// :
finishBeanFactoryInitialization(beanFactory);
// Addition 2
setIsBeingRefreshed(false)
setIsRefreshed(true)
// :
finishRefresh();
}
With that available I would be able to test if isRefreshed() && !isBeingRefreshed() to make sure I have a "stable" and refreshed context.
But no such methods exist so I was wondering if you are aware of anything effectively equivalent.