To quote the spring reference: The ApplicationContextAware callback is invoked after population of normal bean properties but before an initialization callback such as InitializingBeans afterPropertiesSet or a custom init-method.
However, the setApplicationContext(..) is being called after the custom init method of one of my beans. Any idea how can change the flow as I want the CONTEXT to get loaded first and then the init-method should be called by Spring.
This shouldn't happen. Can you reproduce this using a a SSCE ? Can you get a stack trace when the call gets into the setApplicationCOntext and the post construct method?
Joined: Aug 15, 2012
Absolutely, as per the JavaDocs afterPropertiesSet(..) should be called after setApplicationContext(..). But practically this isn't happening and I got NullPointerException as I am using the ApplicationContext in my init() method. I hope the following link would help as to understand why this happens:
The resolution to this is instead of implementing the ApplicationContextAware, it is useful in this case to implement BeanFactoryAware and use the setBeanFactory(..) method.
This method gets called before any custom init methods are executed and in case if you need to use any bean in your init() method, you can do that.
Wow.. good to know. Is the behavior differrent if you use a init method vs afterPropertiesSets vs PostConstruct annotation. I might be wrong here, but I seem to recall the PostConstruct annotated function is called from a PostProcessor, whereas initmethod/afterPropertiesSet are called directly from spring.