Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

EJB Injection and JNDI lookup ?

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

While reading through page# 416 of EJB Code spec (Section: 16.5.1.2 EJB Reference Programming Interfaces), I came across an example which looks like this:

1 @EJB(name=�ejb/EmplRecord�, beanInterface=EmployeeRecordHome.class)
2 @Stateless public class EmployeeServiceBean
3 implements EmployeeService {
4 public void changePhoneNumber(...) {
5 ...
6 // Obtain the default initial JNDI context.
7 Context initCtx = new InitialContext();
8 // Look up the home interface of the EmployeeRecord
9 // enterprise bean in the environment.
10 Object result = initCtx.lookup("java:comp/env/ejb/EmplRecord");
11 // Convert the result to the proper type.
12 EmployeeRecordHome emplRecordHome = (EmployeeRecordHome)
13 javax.rmi.PortableRemoteObject.narrow(result,
14 EmployeeRecordHome.class);
15 ...
16 }
17 }

What I find puzzling here is that code is "injecting" EJB reference (line# 1) and then ALSO performing a JNDI Lookup (line# 10) - why? I thought we should be doing either of the two things and NOT both. Could someone shed some light on this, please? What am I missing here

Many thanks in advance!
Saeed
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that example I don't see the @EJB on an attribute of the class, and it is an interface anyway.

Injection is on attributes.

Mark
 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark but I am confused - what is the effect of Line#1 in the code?

Thanks again!
Saeed
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 1 is class-level (it comes before the class declaration) and gives you the JNDI entry to use to find the home reference by lookup() on Line 10. Notice that Line 1 does NOT give you an object reference.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line#1 says that the JNDI name for the EJB is ejb/EmplRecord. However, you are doing the EJB lookup using the JNDI reference; hence you have the java:comp/env at the front. It is not mandatory to have the JNDI reference. However, it is a good practice. It helps to de-couple your code from deployment time configuration. If you lookup using the JNDI reference rather than by the actual JNDI name, even if the actual JNDI name changes, your code need not change. Ask more questions if you are still not clear...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic