aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Using DI in Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Using DI in Client" Watch "Using DI in Client" New topic
Author

Using DI in Client

krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi,
I have a doubt regarding injecting the Beans in the client code.
Following is the code:



In the above code, I want to get the MyBean instance using DI. I know how to get it through JNDI lookup. But I want to know in which cases I can use DI to get the Bean. I am getting nullpointerException if am using DI. [ ].

Thanks,
Krishna.


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
Mirko Bonasorte
Ranch Hand

Joined: May 14, 2007
Posts: 244
It's the EJB container which implements resources injection for Session beans and MDB.
Everything else is simply ignored: an annotation has no logic at all. The ejb container looks at the annotations on ejbs and injects resources.
If you use annotations in a class of yours which is not explicitly managed by the container, it is simply ignored: for example, nothing prevents you from creating your own annotation class (there's a cool example on O'Reilly book)


SCJP<br />SCWCD 1.4 Upgrade (Remember: me stupid)<br />SCWCD 1.4<br /><a href="http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html" target="_blank" rel="nofollow">SCBCD 5.0</a><br /><a href="http://www.enthuware.com" target="_blank" rel="nofollow">SCBCD 5.0 mock exam</a> <br /> <br />SCEA 5 Part1: Preparing...
Anish Mathur
Greenhorn

Joined: Jun 30, 2008
Posts: 9
DI works only in container managed classes like EJB's, Servlets etc. Therefore, if you client is a managed class then only @EJB annotation can be used.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi,
Look at the below code:


In the above code I am getting Null pointer exception. What I am doing is, I have a jar file consists of Ejb bean, I have a war file consists of this servlet. I am making a Ear file and deployed it in Jboss.

If am using JNDI look up for getting the bean, it is working fine but if i am using @EJB, i am getting Null pointer. Can anyone helps me where I am doing mistake?
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
i don't think you need the remote part in the @EJB(name="Calculator/remote") as you are referring from a servlet. Just try withouth the /remote part and see.


Dinuka


if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi,

No luck dude [ ]!!
I have tried every combination and with out giving the name as it is optional.
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
Hi,
One more thing. If your using JBoss as the app server then i think servlet DI was only supported from version 4.1.x... so if your using an older JBoss version this could be a problem. What is the JBoss app server version you are using?
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi,

I am using JBoss-4.2.2
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
found you problem. found it at JBoss 4.2.2 it says:
However when we started using DI with the latest stable version of JBoss (4.2.2) we found that DI of EJB references has limited support. Namely it is only supported for objects inside the EJB container. This means that while beans can refer to each other with the @EJB annotation, other managed objects like servlets and non-EJB web services must still use JNDI lookup to access any Enterprise Beans.


hope this solves your problem
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Oh..Thank you very much for your precious time....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using DI in Client