SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
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

•

•

http://robert.muntea.nu

Mylyn Connector for
MantisBT
Mylyn Connector for
Review Board

@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 2
Service 1
Service
AppContext 1

http://robert.muntea.nu

@rombert
Decouple collaborators – increase coherence

TextFormatter

EmailSender

SmtpService

RecipientFinder

http://robert.muntea.nu

@rombert
Dependency Injection – walkthrough

Image courtesy of FreeFoto.com

http://robert.muntea.nu

@rombert
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
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

http://robert.muntea.nu

@rombert

Contenu connexe

En vedette

Apache Sling as a Microservices Gateway
Apache Sling as a Microservices GatewayApache Sling as a Microservices Gateway
Apache Sling as a Microservices GatewayRobert Munteanu
 
Spring Dynamic Modules for OSGi by Example - Martin Lippert, Consultant
Spring Dynamic Modules for OSGi by Example - Martin Lippert, ConsultantSpring Dynamic Modules for OSGi by Example - Martin Lippert, Consultant
Spring Dynamic Modules for OSGi by Example - Martin Lippert, Consultantmfrancis
 
Ciclo basico diurno vigencia 2009 scp
Ciclo basico diurno vigencia 2009 scpCiclo basico diurno vigencia 2009 scp
Ciclo basico diurno vigencia 2009 scpRuth Santana
 
Horizons Beyond Dreams Business Plan
Horizons Beyond Dreams Business PlanHorizons Beyond Dreams Business Plan
Horizons Beyond Dreams Business Planphillipcfrankis
 
The Praying Indians of Megunko
The Praying Indians of MegunkoThe Praying Indians of Megunko
The Praying Indians of Megunkopebrodeur
 
ROR -Igal Assaf Paris sous la neige
ROR -Igal Assaf  Paris sous la neigeROR -Igal Assaf  Paris sous la neige
ROR -Igal Assaf Paris sous la neigeIgal Assaf
 
Text 4 superlatives ok
Text 4 superlatives okText 4 superlatives ok
Text 4 superlatives okIES EUROPA
 
Exposing Opportunities in China A50 using CFD
Exposing Opportunities in China A50 using CFDExposing Opportunities in China A50 using CFD
Exposing Opportunities in China A50 using CFDPhillip CFD
 
Where can tell me who I am?
Where can tell me who I am?Where can tell me who I am?
Where can tell me who I am?seltzoid
 
Blackwell Esteem AFSL
Blackwell Esteem AFSLBlackwell Esteem AFSL
Blackwell Esteem AFSLsamueltay77
 
Jill lintner's portfolio
Jill lintner's portfolioJill lintner's portfolio
Jill lintner's portfolioocwebservices
 

En vedette (17)

Apache Sling as a Microservices Gateway
Apache Sling as a Microservices GatewayApache Sling as a Microservices Gateway
Apache Sling as a Microservices Gateway
 
Spring Dynamic Modules for OSGi by Example - Martin Lippert, Consultant
Spring Dynamic Modules for OSGi by Example - Martin Lippert, ConsultantSpring Dynamic Modules for OSGi by Example - Martin Lippert, Consultant
Spring Dynamic Modules for OSGi by Example - Martin Lippert, Consultant
 
Ciclo basico diurno vigencia 2009 scp
Ciclo basico diurno vigencia 2009 scpCiclo basico diurno vigencia 2009 scp
Ciclo basico diurno vigencia 2009 scp
 
rajesh - Updated
rajesh - Updatedrajesh - Updated
rajesh - Updated
 
Horizons Beyond Dreams Business Plan
Horizons Beyond Dreams Business PlanHorizons Beyond Dreams Business Plan
Horizons Beyond Dreams Business Plan
 
The Praying Indians of Megunko
The Praying Indians of MegunkoThe Praying Indians of Megunko
The Praying Indians of Megunko
 
ROR -Igal Assaf Paris sous la neige
ROR -Igal Assaf  Paris sous la neigeROR -Igal Assaf  Paris sous la neige
ROR -Igal Assaf Paris sous la neige
 
Text 4 superlatives ok
Text 4 superlatives okText 4 superlatives ok
Text 4 superlatives ok
 
Report..costing..
Report..costing..Report..costing..
Report..costing..
 
Exposing Opportunities in China A50 using CFD
Exposing Opportunities in China A50 using CFDExposing Opportunities in China A50 using CFD
Exposing Opportunities in China A50 using CFD
 
SGGSWU Prospectus 2016-17
SGGSWU Prospectus 2016-17SGGSWU Prospectus 2016-17
SGGSWU Prospectus 2016-17
 
Where can tell me who I am?
Where can tell me who I am?Where can tell me who I am?
Where can tell me who I am?
 
Blackwell Esteem AFSL
Blackwell Esteem AFSLBlackwell Esteem AFSL
Blackwell Esteem AFSL
 
Li & fung ltd
Li & fung ltdLi & fung ltd
Li & fung ltd
 
Jill lintner's portfolio
Jill lintner's portfolioJill lintner's portfolio
Jill lintner's portfolio
 
sdfghjk
sdfghjksdfghjk
sdfghjk
 
Good prescribing
Good prescribingGood prescribing
Good prescribing
 

Plus de Robert Munteanu

Secure by Default Web Applications
Secure by Default Web ApplicationsSecure by Default Web Applications
Secure by Default Web ApplicationsRobert Munteanu
 
Sling Applications - A DevOps perspective
Sling Applications - A DevOps perspectiveSling Applications - A DevOps perspective
Sling Applications - A DevOps perspectiveRobert Munteanu
 
Will it blend? Java agents and OSGi
Will it blend? Java agents and OSGiWill it blend? Java agents and OSGi
Will it blend? Java agents and OSGiRobert Munteanu
 
Escape the defaults - Configure Sling like AEM as a Cloud Service
Escape the defaults - Configure Sling like AEM as a Cloud ServiceEscape the defaults - Configure Sling like AEM as a Cloud Service
Escape the defaults - Configure Sling like AEM as a Cloud ServiceRobert Munteanu
 
Crash course in Kubernetes monitoring
Crash course in Kubernetes monitoringCrash course in Kubernetes monitoring
Crash course in Kubernetes monitoringRobert Munteanu
 
Java agents for fun and (not so much) profit
Java agents for fun and (not so much) profitJava agents for fun and (not so much) profit
Java agents for fun and (not so much) profitRobert Munteanu
 
Will it blend? Java agents and OSGi
Will it blend? Java agents and OSGiWill it blend? Java agents and OSGi
Will it blend? Java agents and OSGiRobert Munteanu
 
Cloud-native legacy applications
Cloud-native legacy applicationsCloud-native legacy applications
Cloud-native legacy applicationsRobert Munteanu
 
From Monolith to Modules - breaking apart a one size fits all product into mo...
From Monolith to Modules - breaking apart a one size fits all product into mo...From Monolith to Modules - breaking apart a one size fits all product into mo...
From Monolith to Modules - breaking apart a one size fits all product into mo...Robert Munteanu
 
What's new in the Sling developer tooling?
What's new in the Sling developer tooling?What's new in the Sling developer tooling?
What's new in the Sling developer tooling?Robert Munteanu
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code baseRobert Munteanu
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code baseRobert Munteanu
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code baseRobert Munteanu
 
Zero downtime deployments for Sling application using Docker
Zero downtime deployments for Sling application using DockerZero downtime deployments for Sling application using Docker
Zero downtime deployments for Sling application using DockerRobert Munteanu
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code baseRobert Munteanu
 
Secure by Default Web Applications with Apache Sling
Secure by Default Web Applications with Apache SlingSecure by Default Web Applications with Apache Sling
Secure by Default Web Applications with Apache SlingRobert Munteanu
 
Effective Web Application Development with Apache Sling
Effective Web Application Development with Apache SlingEffective Web Application Development with Apache Sling
Effective Web Application Development with Apache SlingRobert Munteanu
 
Building domain-specific testing tools : lessons learned from the Apache Slin...
Building domain-specific testing tools : lessons learned from the Apache Slin...Building domain-specific testing tools : lessons learned from the Apache Slin...
Building domain-specific testing tools : lessons learned from the Apache Slin...Robert Munteanu
 
So how do I test my Sling application?
 So how do I test my Sling application? So how do I test my Sling application?
So how do I test my Sling application?Robert Munteanu
 

Plus de Robert Munteanu (20)

Secure by Default Web Applications
Secure by Default Web ApplicationsSecure by Default Web Applications
Secure by Default Web Applications
 
Sling Applications - A DevOps perspective
Sling Applications - A DevOps perspectiveSling Applications - A DevOps perspective
Sling Applications - A DevOps perspective
 
Will it blend? Java agents and OSGi
Will it blend? Java agents and OSGiWill it blend? Java agents and OSGi
Will it blend? Java agents and OSGi
 
Escape the defaults - Configure Sling like AEM as a Cloud Service
Escape the defaults - Configure Sling like AEM as a Cloud ServiceEscape the defaults - Configure Sling like AEM as a Cloud Service
Escape the defaults - Configure Sling like AEM as a Cloud Service
 
Crash course in Kubernetes monitoring
Crash course in Kubernetes monitoringCrash course in Kubernetes monitoring
Crash course in Kubernetes monitoring
 
Java agents for fun and (not so much) profit
Java agents for fun and (not so much) profitJava agents for fun and (not so much) profit
Java agents for fun and (not so much) profit
 
Will it blend? Java agents and OSGi
Will it blend? Java agents and OSGiWill it blend? Java agents and OSGi
Will it blend? Java agents and OSGi
 
Cloud-native legacy applications
Cloud-native legacy applicationsCloud-native legacy applications
Cloud-native legacy applications
 
Cloud-Native Sling
Cloud-Native SlingCloud-Native Sling
Cloud-Native Sling
 
From Monolith to Modules - breaking apart a one size fits all product into mo...
From Monolith to Modules - breaking apart a one size fits all product into mo...From Monolith to Modules - breaking apart a one size fits all product into mo...
From Monolith to Modules - breaking apart a one size fits all product into mo...
 
What's new in the Sling developer tooling?
What's new in the Sling developer tooling?What's new in the Sling developer tooling?
What's new in the Sling developer tooling?
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code base
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code base
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code base
 
Zero downtime deployments for Sling application using Docker
Zero downtime deployments for Sling application using DockerZero downtime deployments for Sling application using Docker
Zero downtime deployments for Sling application using Docker
 
Scaling up development of a modular code base
Scaling up development of a modular code baseScaling up development of a modular code base
Scaling up development of a modular code base
 
Secure by Default Web Applications with Apache Sling
Secure by Default Web Applications with Apache SlingSecure by Default Web Applications with Apache Sling
Secure by Default Web Applications with Apache Sling
 
Effective Web Application Development with Apache Sling
Effective Web Application Development with Apache SlingEffective Web Application Development with Apache Sling
Effective Web Application Development with Apache Sling
 
Building domain-specific testing tools : lessons learned from the Apache Slin...
Building domain-specific testing tools : lessons learned from the Apache Slin...Building domain-specific testing tools : lessons learned from the Apache Slin...
Building domain-specific testing tools : lessons learned from the Apache Slin...
 
So how do I test my Sling application?
 So how do I test my Sling application? So how do I test my Sling application?
So how do I test my Sling application?
 

Dernier

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 

Dernier (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Dependency injection: Spring, Guice, OSGi

  • 1. Dependency Injection – Spring, Guice, OSGi Dependency Injection – Spring, Guice, OSGi Robert Munteanu, Adobe Systems Inc http://robert.muntea.nu @rombert
  • 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. 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. Accidental complexity – locating a service ctx = new InitialContext(); ds = (DataSource)ctx.lookup("jdbc/fastCoffeeDB"); http://robert.muntea.nu @rombert
  • 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. 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. Decouple collaborators – increase reusability Service 2 Service 1 Service AppContext 1 http://robert.muntea.nu @rombert
  • 8. Decouple collaborators – increase coherence TextFormatter EmailSender SmtpService RecipientFinder http://robert.muntea.nu @rombert
  • 9. Dependency Injection – walkthrough Image courtesy of FreeFoto.com http://robert.muntea.nu @rombert
  • 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. 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. 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. 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. Dependency Injection – getting a hold of services Q&A http://robert.muntea.nu @rombert