File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Injecting EJB's using DI 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 "Injecting EJB Watch "Injecting EJB New topic
Author

Injecting EJB's using DI

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Guys,

As per the specification, the EJB annotation can be used only in managed classes of the EJB container. Can I inject a EJB resource in a Servlet? The EJB specification for the EJB container does not say that it should support Servlets & JSP. Any ideas?


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9323
    
109

Jothi Shankar Kumar wrote: Can I inject a EJB resource in a Servlet?


Yes, you can.

[My Blog] [JavaRanch Journal]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Yes, you can.

But this is new to the Servlet 3.0 API, right? And to my knowledge you can define EJB resource to be available to concurrently used components like Servlets but you still have to look it up via JNDI. Please correct me if I'm wrong!

Marco
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9323
    
109

Marco Ehrentreich wrote:
Yes, you can.

But this is new to the Servlet 3.0 API, right?


Not really. It was available even in JavaEE5 (which was Servlet 2.x).
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9323
    
109

Marco Ehrentreich wrote: And to my knowledge you can define EJB resource to be available to concurrently used components like Servlets but you still have to look it up via JNDI. Please correct me if I'm wrong!

Marco


Sorry, i can't seem to understand this question Can you please elaborate?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

It's read this in Servlet 3.0Final Spec, 15.5.3 @EJBs Annotation

It says basically that you can annotate Servlet classes with one or more @EJB annotations just to declare that some EJBs should be available to this servlet class. But you still have to actively get a reference to the allowed EJBs via JNDI. It also says that this is just a convenient way to avoid that you have to declare the accessible EJBs in the web.xml descriptor.

Above this in the spec is an example with only one @EJB injection into a member variable in a servlet where it notes nothing about JNDI. Maybe the JNDI lookup is only necessary if you declare multiple @EJBs which wouldn't make much sense to me

Sorry that I can't tell you more about this but I don't really understand it in detail from this short explanation in the spec. Maybe you're willing to take the time to have a look into the specification yourself...

Marco
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9323
    
109

Note that the @EJBs annotation is different from the @EJB annotation. The section 15.5.3 of the servlet spec talks about the @EJBs annotation. The @EJBs annotation is used to make available one or more Enterprise Java Beans in the java:comp/env namespace of a component (in this case the servlet). This is unlike the @EJB annotation which *injects* a bean reference in a field. The @EJBs are used to just make the beans available in the java:comp/env namespace of the servlet so that later on, in the servlet component you can do something like:


If you are familiar with the usage of <ejb-ref> in web.xml, then the @EJBs are equivalent.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Oops, obviously I didn't get the point with the "s" in the name of the annotation when I read the spec document last night But thanks a lot for pointing me to this difference!

There's still one thing I don't understand completely. It's not allowed to inject stateful EJBs into stateless ones because the container couldn't guarantee that a client sees the correct injected EJB when SLSBs are shared between different clients. How does this work for Servlets which are usually reused and shared between multiple clients, too? Or are there restrictions on what kind of EJBs you are allowed to request?

Thanks in advance!

Marco
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Jaikiran Pai wrote:
Jothi Shankar Kumar wrote: Can I inject a EJB resource in a Servlet?


Yes, you can.


Does this mean that the Servlet is a managed resource of the EJB Container?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9323
    
109

Marco Ehrentreich wrote:How does this work for Servlets which are usually reused and shared between multiple clients, too?



It works in a similar way. Injecting SFSB in servlet is not recommended.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Thanks a lot, Jaikiran! That makes things clearer.

Marco
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Hi Jothi,
Does this mean that the Servlet is a managed resource of the EJB Container?

I guess, otherwise DI wouldn't really work. Or do you mean anything else by "managed"?

Marco
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

We cannot use the EJB annotation in a class which is not managed by the EJB container. Am I right? In this sense, can we use this annotation in a Servlet?? Is the Servlet managed by the EJB Container?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

EJBs and Servlets are both managed by an application server even though there may be usually distinct sub-containers for EJBs and Servlets respectively. But without the application server knowing about both components DI of EJB resources of course wouldn't work here as expected. (Maybe it would compile with the JARs containing the annotation declarations in place but it surely wouldn't inject anything at runtime).

Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Injecting EJB's using DI
 
Similar Threads
give link for EJB specification
Help??(Can we use wait method in Bean Class)
Allow single transaction only
What is the different between EJB server and EJB container?
NeedJ2EE 1.3 RI server Installation Help