Dependency injection: Spring, Guice, OSGi

Post on 27-May-2015

2.389 views 5 download

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

http://robert.muntea.nu @rombert

Dependency Injection – Spring, Guice, OSGi

Dependency Injection – Spring, Guice, OSGi

Robert Munteanu, Adobe Systems Inc

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

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

http://robert.muntea.nu @rombert

Accidental complexity – locating a service

ctx = new InitialContext();

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

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

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

http://robert.muntea.nu @rombert

Decouple collaborators – increase reusability

Service

AppContext 1

Service 1

Service 2

http://robert.muntea.nu @rombert

Decouple collaborators – increase coherence

EmailSender

TextFormatter

RecipientFinder

SmtpService

http://robert.muntea.nu @rombert

Dependency Injection – walkthrough

Image courtesy of FreeFoto.com

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

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?

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

http://robert.muntea.nu @rombert

Dependency Injection – resources

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

Dependency Injection – Dhanji R. Prasanna

Java Application Arhitecture – Kirk Knoernschild

http://robert.muntea.nu @rombert

Dependency Injection – getting a hold of services

Q&A