Few readers wrote me asking how can they invoke EJB 3.0 session beans from web container. I have been helping one of the customers who have been trying to port an application from JBoss EJB 3.0 implementation to OC4J 10.1.3 DP4 and apparently they were using JBoss’s proprietary extension of looking by class name similar to OC4J’s ability to lookup by ejb-name specified in the deployment descriptor. It's worth mentioning that Java EE specification can ensure portability of applications only by explicitly defining the dependencies on EJBs by using ejb-ref or ejb-local-ref and then looking using the ref-name to lookup the EJB.
For example, if you want to lookup an EJB 3.0 session bean from a Servlet or JSP then you can define the dependency on the EJB as follows:
<ejb-local-ref> <ejb-ref-name>ejb/HelloWorld</ejb-ref-name> <local>oracle.ejb30.HelloWorld</local> </ejb-local-ref>
Then lookup the object using JNDI as follows before we can use the resource.
Context ic = new InitialContext();
HelloWorld helloWorld = (HelloWorld)ic.lookup("java:comp/env/ejb/HelloWorld");
helloWorld.sayHello("Debu");
If you are using EJB from another Session bean or MDB you may use dependency injection to get a reference to another EJB instead of you using JNDI lookup as follows:
@EJB private HelloWorld helloWorld; helloWorld.sayHello("Debu");
We currently do not support dependency injection in web container and hoping Java EE 5.0 supports dependency injection in the web container you should be able use the above code in your Servlet classes in a Java EE 5.0 compliant container.
12:12:25 PM
|