Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

@EJB annotation on field vs. class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain to me the difference between annotating a field in an EJB with @EJB and annotating the EJB class with the @EJB annotation and how it relates to the ENC?

To my understanding, annotating a field with @EJB injects a reference to the EJB into the field. Does it look up in the ENC to do this injection? Is all injection done from the ENC?

What does annotating a bean class with @EJB do?

Some clarification on these topics would be greatly appreciated.

Thanks.
 
Ranch Hand
Posts: 138
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajay,

As you mentioned, annotating a field with @EJB injects EJB reference by looking up the JNDI name( specified by lookup property of @EJB)
@EJB on EJB class registers the EJB with a specific JNDI name (though it is registered by a default name in absence of 'name' property)
 
Ajay Dhar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your reply

So you are saying that using the @EJB annotation with the name property on the class registers that particular EJB in the ENC with the provided name. So, that means that the @EJBs annotation shouldn't be used at the class level because there is only one EJB that you can register, and that is the annotated EJB. Is this right? I'm not sure. Because it doesn't make sense to use the @EJBs annotation for a field because you can only inject one EJB into a given field. So when do you use the @EJBs annotation?
 
Ajay Dhar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a little more reading and tinkering with code and I figured something out. Ok, the @Resource annotation applied to a field injects the resource into that field. The @Resource annotation applied to the class adds the particular resource specified in the annotation into the ENC for the annotated EJB. The EJB can then look up that resource using JNDI and a JNDI name of "java:/comp/env/...". Each EJB in an EJB jar has its own ENC and a WAR has one ENC for everything in the WAR.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic