Dependency injection: Spring, Guice, OSGi

3,800 views

Published on

A comparative look at three popular dependency injection frameworks: Spring, Guice, and OSGi declarative services.

Supporting code is found at https://github.com/rombert/email-sender .

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,800
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Dependency injection: Spring, Guice, OSGi

  1. 1. Dependency Injection – Spring, Guice, OSGi Dependency Injection – Spring, Guice, OSGi Robert Munteanu, Adobe Systems Inc http://robert.muntea.nu @rombert
  2. 2. $ whoami $DAYJOB • Computer Scientist at Adobe Systems Inc FOSS • Apache Sling • MantisBT • • http://robert.muntea.nu Mylyn Connector for MantisBT Mylyn Connector for Review Board @rombert
  3. 3. Why Dependency Injection? • Drive out accidental complexity • • Leave the boilerplate to others Decouple collaborators • Increase reusability • Ease testing • Increase coherence http://robert.muntea.nu @rombert
  4. 4. Accidental complexity – locating a service ctx = new InitialContext(); ds = (DataSource)ctx.lookup("jdbc/fastCoffeeDB"); http://robert.muntea.nu @rombert
  5. 5. Accidental complexity – manually wiring components DataSource ds = getDataSource(); // OK impl ;-) AddressDao dao = new AddressDao(ds); PersistenceComponent pc = new PersistenceComponent(dao); ServerConfig cfg = new ServerConfig(props); ServerComponent sc = new ServerComponent(cfg); Application app = new Application(Arrays.asList(pc, sc)); app.start(); http://robert.muntea.nu @rombert
  6. 6. Decouple collaborators – ease testing public class ServiceMonitor { public notifyIfDown() { Service s = ServiceFactory.getServiceToCheck(); try { s.connect() } catch ( ServiceException e) { ServiceFactory.getNotificationService().notifyOf (e); } } } http://robert.muntea.nu @rombert
  7. 7. Decouple collaborators – increase reusability Service 2 Service 1 Service AppContext 1 http://robert.muntea.nu @rombert
  8. 8. Decouple collaborators – increase coherence TextFormatter EmailSender SmtpService RecipientFinder http://robert.muntea.nu @rombert
  9. 9. Dependency Injection – walkthrough Image courtesy of FreeFoto.com http://robert.muntea.nu @rombert
  10. 10. Dependency Injection – walkthrough Email Sender App Technologies Deliver formatted email to large number of recipients Spring 3.2 - JavaConfig http://robert.muntea.nu Guice 3.0 OSGi DS 1.6.2 @rombert
  11. 11. Dependency Injection – bottom line All frameworks prevent accidental complexity • Different binding approaches • • Spring binds beans • Guice and OSGi bind services to consumers Different approaches to run-time composition • • Spring and Guice have a static view of the world • OSGi is dynamic by definition • Which view is right for you? http://robert.muntea.nu @rombert
  12. 12. Dependency Injection – bottom line Different approaches to scopes • • Spring and OSGi default to singleton • Guice defaults to instance Different approaches to instantation • • Spring and Guice default to eager • OSGi (SCR) defaults to lazy http://robert.muntea.nu @rombert
  13. 13. Dependency Injection – resources https://github.com/rombert/email-sender Dependency Injection – Dhanji R. Prasanna Java Application Arhitecture – Kirk Knoernschild http://robert.muntea.nu @rombert
  14. 14. Dependency Injection – getting a hold of services Q&A http://robert.muntea.nu @rombert

×