aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes ApplicationContextAware vs @PostConstruct - flow of control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "ApplicationContextAware vs @PostConstruct - flow of control" Watch "ApplicationContextAware vs @PostConstruct - flow of control" New topic
Author

ApplicationContextAware vs @PostConstruct - flow of control

Sid Moitra
Greenhorn

Joined: Aug 15, 2012
Posts: 6
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.

Any views on this??
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2451
    
  28

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?
Sid Moitra
Greenhorn

Joined: Aug 15, 2012
Posts: 6
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:
http://osdir.com/ml/java.springframework.devel/2004-01/msg00044.html

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.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2451
    
  28

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.
 
 
subject: ApplicationContextAware vs @PostConstruct - flow of control