File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes service locator pattern and DI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "service locator pattern and DI" Watch "service locator pattern and DI" New topic

service locator pattern and DI

satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
With the new ejb 3 spec and introduction of DI would it be safe to say that service locator pattern usage would be eliminated / decreased ?

My Take :; DI not possible in a non managed class and there we are forced to use JNDI

But then - will there be a scenario to need to call an EJB from a non managed class ?

Does JSP allow DI ?
Thanks ,
[ August 08, 2008: Message edited by: satish bodas ]
aleem khan
Ranch Hand

Joined: Aug 07, 2008
Posts: 94
Yes there will be situation where non managed class has to access EJB, in that case you have to use JNDI

SCJP(1.4), SCWCD, Oracle 9i SQL certified, Oracle PLSQL Developer Certified Associate
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
JSP don't allow DI, but I was able to do it in Tomcat 6.0 and with declaring the particular JSP as a servlet in web.xml. More here:
Ashwin Pai
Ranch Hand

Joined: May 20, 2008
Posts: 90
To me DI is a Service Locator pattern implemented by the Container!!
However until DI is made availbale in non-managed classes too, app developer will have to take the responsibility of implementing Service Locator pattern.


The only way to make your dream come true is to wake up.
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Service Locator pattern is still need for web frameworks that does not support DI.

An alternative for such is simply let the EJB container inject instances of the session beans (whether remote or local) into a servlet which are declared as instance variables, override the init and load the servlet at startup. Now you have a service locator POJO (singleton that wraps a synchornized map) that already caches the instances of the session beans for the perusal of other classes.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
Reza Rahman
Ranch Hand

Joined: Feb 01, 2005
Posts: 580

You may use Seam, Spring or Guice to inject resources from JNDI (including EJBs) into non-managed classes. Java EE 6 standardizes this via WebBeans. However, there are many people using Java EE 5 that just use look-ups for this case since it does not happen that often.

Best regards,

Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
I agree. Here's the link:
subject: service locator pattern and DI
It's not a secret anymore!