aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Question reagarding beanName attribute of @EJB annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Question reagarding beanName attribute of @EJB annotation" Watch "Question reagarding beanName attribute of @EJB annotation" New topic
Author

Question reagarding beanName attribute of @EJB annotation

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Source : Enterprise JavaBeans 3.1 O'Reilly Sixth Edition

Chapter 16: JNDI, the ENC, and Injection


@javax.ejb.EJB (Page:264)


The beanName()is the EJB name of the EJB referenced. It is equal to either the value you specify in the @Stateless.name()or @Stateful.name()annotation or the value you put in the <ejb-name>field in the XML deployment descriptor

Suppose MyEJB wants a reference to the local business interface of MyEJB2. Then, as per the above statement(The beanName()is the EJB name of the EJB referenced)
the beanName should be equal to "MyEJB2".
However,its also written that its value can be equal to "value you put in the <ejb-name>field in the XML deployment descriptor".
The value put in the <ejb-name> is the name of the EJB that wants a reference to some other bean.(Here,MyEJB)

Question 1:How can the 2 statements in BOLD be true.They seem to be contradictory ?

OCPJP 6.0 93%
OCPJWCD 5.0 98%
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

The examples in the OReilly book are not showing you the complete picture, it costed me also some time to figure it out, but here are the two adjusted examples:

or in xml

Regards,
Frits
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Thanks Frits.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

i couldnt understand the purpose of name attribute in @EJB annotation and the corresponding <ejb-ref-name> DD tag. can somebody please explain with an example >
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

name attribute in @EJB annotation and the corresponding <ejb-ref-name> DD tag

That name is to be used when you do a lookup inside the MyEJBBean class (it is actually a short name)
for example:

Regards,
Frits
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

@EJB(name="ejbs/referenceToMyEJB2", beanInterface=MyEJB2LocalBusiness.class, beanName="MyEJB2") // here is the reference



so is it like that whatever name is specify in name attribute of @EJB annotation gets added to the java:comp/env namespace of the bean from which I am doing injection(in this case MyEjbBean) ? is it mandatory to specify beanName attribute always ? if there is just one session bean that implements whatever interface we give in beanInterface attribure, can I omit beanName ?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

so is it like that whatever name is specify in name attribute of @EJB annotation gets added to the java:comp/env namespace of the bean from which I am doing injection(in this case MyEjbBean) ?

Correct.
is it mandatory to specify beanName attribute always ?
No
if there is just one session bean that implements whatever interface we give in beanInterface attribure, can I omit beanName ?

Exactly: you are getting the picture!

Just to confirm from the API:
javax.ejb Annotation Type EJB
Either the beanName or the lookup element can be used to resolve the EJB dependency to its target session bean component. It is an error to specify values for both beanName and lookup.

If no explicit linking information is provided and there is only one session bean within the same application that exposes the matching client view type, by default the EJB dependency resolves to that session bean.

Just have a look at the picture explaining the difference between beanName and lookup (My notes, chaper 7.2 @EJB - beanName vs lookup)

Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question reagarding beanName attribute of @EJB annotation