• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question reagarding beanName attribute of @EJB annotation  RSS feed

 
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ?
 
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 3064
248
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits.
 
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 3064
248
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@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
Saloon Keeper
Posts: 3064
248
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!