I've tried to create a Spring bean and log all possible stages of his lifecycle.
But I've noticed that in this case a method annotated with @PostConstruct
isn't invoked.
I suppose it happens because BeanFactoryPostProcessor
and ApplicationContextAware
force container to instantiate it as early as possible, but I don't know exactly.
Could you explain me, please?
Please consider a code snippet below:
public class F implements BeanFactoryPostProcessor, ApplicationContextAware {
@PostConstruct
private void postConstruct() {
System.out.println("Hi from postContruct()");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("Hi from setApplicationContext()");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("Hi from postProcessBeanFactory()");
}
}
And the output is:
Hi from setApplicationContext()
Hi from postProcessBeanFactory()