Dependency injection: Spring, Guice, OSGi

14

Click here to load reader

description

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 .

Transcript of Dependency injection: Spring, Guice, OSGi

Page 1: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Dependency Injection – Spring, Guice, OSGi

Dependency Injection – Spring, Guice, OSGi

Robert Munteanu, Adobe Systems Inc

Page 2: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

$ whoami

$DAYJOB• Computer Scientist at

Adobe Systems Inc

FOSS• Apache Sling• MantisBT• Mylyn Connector for

MantisBT• Mylyn Connector for

Review Board

Page 3: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Why Dependency Injection?

• Drive out accidental complexity• Leave the boilerplate to others

• Decouple collaborators• Increase reusability• Ease testing• Increase coherence

Page 4: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Accidental complexity – locating a service

ctx = new InitialContext();

ds = (DataSource)ctx.lookup("jdbc/fastCoffeeDB");

Page 5: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

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();

Page 6: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Decouple collaborators – ease testing

public class ServiceMonitor { public notifyIfDown() { Service s = ServiceFactory.getServiceToCheck(); try { s.connect() } catch ( ServiceException e) { ServiceFactory.getNotificationService().notifyOf(e); } }}

Page 7: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Decouple collaborators – increase reusability

Service

AppContext 1

Service 1

Service 2

Page 8: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Decouple collaborators – increase coherence

EmailSender

TextFormatter

RecipientFinder

SmtpService

Page 9: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Dependency Injection – walkthrough

Image courtesy of FreeFoto.com

Page 10: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Dependency Injection – walkthrough

Email Sender App

Deliver formatted email to large number of recipients

Technologies

Spring 3.2 - JavaConfig

Guice 3.0

OSGi DS 1.6.2

Page 11: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

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?

Page 12: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

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

Page 13: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Dependency Injection – resources

https://github.com/rombert/email-sender

Dependency Injection – Dhanji R. Prasanna

Java Application Arhitecture – Kirk Knoernschild

Page 14: Dependency injection: Spring, Guice, OSGi

http://robert.muntea.nu @rombert

Dependency Injection – getting a hold of services

Q&A