18 January 2010

Calling session beans on JSF Managed bean's start up

One situation may occurs is that you need to call some session bean method on some JSF page's start up.

If you try to put the code that calls the injected EJB session bean's method in your JSF request-scoped Managed bean constructor, you will get a NullPointerException as the Injection occurred after making an instance of the managed bean.

The solution is that to provide a @PostConsruct method and put your EJB calling code in.
@PostConsruct is supported in JSF1.2 and if you running your JSF Application in an Application Server (example JBoss)

So, Here's the sequence the managed bean instance is created in:

Calling manged bean constructor => Injecting Session bean (if any) => calling PostConstruct method(s) (if any)

Here's an example:

public class ShowArticleBean {

private ArticleManagement articleMgmt;

public ShowArticleBean() {
System.out.println("In Constructor : " + articleMgmt);


public void construct() {
System.out.println("In PostConsruct : " + articleMgmt);

And here's the output :

04:51:29,546 INFO [STDOUT] In Constructor : null
04:51:29,547 INFO [STDOUT] In PostConsruct : ArticleManagementBean
Post a Comment