Pages

Wednesday, April 28, 2010

EJB injection in a JSF Managed Bean

EJB injection in a managed bean of a JSF application deployed on a Weblogic Server can be tricky, Lucas already made a good blogpost about this subject and provided a workaround. In this blogpost I will show how you can inject a local and remote EJB Session Bean which are deployed with the Web Archive (WAR) in one Enterprise Archive ( EAR) and I injected a remote EJB deployed with a different EAR.
The First and most important step is It only works when you define the Managed Bean in the faces-config.xml and not as an ADF Managed Bean ( like in an unbounded or bounded Task Flow xml )
Here an example of a managed bean configuration

Before you deploy the JSF web application on a Weblogic Server you need to make an EJB deployment profile of your EJB model project and add this to the EAR assembly in the application menu of your Workspace. This is not necessary when you run it in the integrated Weblogic Server of JDeveloper.

When you deploy the EJB deployment profile in the same EAR as the WAR then you can use the @EJB annotation on your local or remote interface variable. As extra you can add the name and mappedName attribute, these values are the same as the attributes of the Stateless annotation of your Session Bean.

An other not recomended way is to provide the beanName attribute and provide the EJB deployment jar location on the WLS server together with a hash and the name of the Bean. Don't know if this works on your integrated Weblogic server of JDeveloper.

And the last way to inject a Remote EJB is to define an ejb-ref element in the web.xml with the injection managed bean class and the variable inside this class and weblogic.xml for the JNDI name of the Remote EJB. This works perfectly when the Remote EJB is not deployed in the same EAR as the Web Application.

No comments:

Post a Comment

ex Oracle ACE , Java Developer of the year 2009. Co-writer of the OSB Development Cookbook.I am working as a Software Architect and specialized in CI,CD, integration, middleware, security and web development.