• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

service locator pattern and DI

 
satish bodas
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ,
~satish
[ August 08, 2008: Message edited by: satish bodas ]
 
aleem khan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there will be situation where non managed class has to access EJB, in that case you have to use JNDI
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://www.coderanch.com/t/414899/EJB-JEE/java/Glassfish-EJB-web-client-EJB
 
Ashwin Pai
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HTH
Thanks
Ashwin
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards.
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish,

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,
Reza
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic