Wednesday, March 21, 2007

Using @EJB annotation across different ejb-jar module

I got some questions from customers on how to use @EJB annotation to inject instance of a session packaged in another EJB-JAR. In this blog entry I will outline how to do that.

Let us assume that you have two stateless beans SessionEJB1 and SessionEJB2 and those are packaged in two different EJB modules ejb1-ejb.jar and ejb2-ejb.jar respectively. You want to use inject SessionEJB2 (in ejb2-ejb.jar) from SessionEJB1 (packaged in ejb1-ejb.jar) then you can just do the following:

@Stateless
public class SessionEJB1Bean implements SessionEJB1, SessionEJB1Local {
@EJB
testejb2.SessionEJB2 ejb2;

This works great in OC4J 10.1.3.1.

Optionally you may do as follows, following the ejb-link convention that we used in EJB 2.

@EJB(beanName="ejb2-ejb.jar#SessionEJB2Bean",
beanInterface=testejb2.SessionEJB2.class)
testejb2.SessionEJB2 ejb2;


I've seen this problem when I deploy from JDeveloper. If you are using JDeveloper 10.1.3.1 and getting NameNotFoundException then make sure that your dependent ejb-jar module does package duplicate classes. By default when you declare a dependency on another project the classes are automatically packaged in the dependent jar e.g. ejb1-ejb.jar in our case. You have to verify it using the deployment profile for your EJB module. Make sure that you have unchecked the classes by selecting filters.

Hope this works for you!

4 comments:

Anil Datt said...

The @EJB anotation works fine when two ejb's (.jars) are bundled in one application (.ear) file. The issue is referencing the (standalone)EJB deployed in another EAR file. So far I could not find a way to refence it using the JNDI name and using java:comp lookup..

Anil

Anonymous said...

Any solution to this having to EJB JAR's not in the same EAR?

domenico said...

I also have the same problems of referencig ejb deployed in another EAR. Any suggestions?

navya said...

Interesting Article

EJB 3 Online Training | Java Online Training

Java Online Training from India | Core Java Online Training