SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Implement Dependency Injection
in Java
Plan
● TDD
o A test from Guice’s user's guide
(https://github.com/google/guice/wiki/Motivation)
o A DI implementation to pass the test
https://github.com/gdhuang/di_demo
Dependency injection
- A design pattern in which dependencies are injected
into a dependent object
Client.java
public Client(Service1 s1, Service2 s2)
Main.java
Service1 s1 = new Service1Imp();
Service2 s2= new Service2Imp();
Client c = new Client(s1, s2);
Guice
● Module - define bindings from types to implementations
● @Inject - specify a constructor is invoked by Guice
Test
● RealBillingServiceTest
Implementation
● Build a rule table of bindings
● Create an object respecting the bindings
Rule table
● Module
● RuleBuilder
● Guice
Injection
● resolve binding
● create an object using reflection
Conclusion
● Java
o Reflection http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/package-summary.html
o Generic http://www.drdobbs.com/java-generics-and-c-templates/184401818
● TDD
o Add necessary tests before implementing furthur than existing tests
o Practice with mockito http://stackoverflow.com/questions/7104485/how-does-jmock-and-
mockito-create-wrappers

Weitere ähnliche Inhalte

Was ist angesagt?

Eclipse Buildship JUG Hamburg
Eclipse Buildship JUG HamburgEclipse Buildship JUG Hamburg
Eclipse Buildship JUG Hamburgsimonscholz
 
Introduction to git flow
Introduction to git flowIntroduction to git flow
Introduction to git flowKnoldus Inc.
 
Occ iware docker-connector-demo-june-2015
Occ iware docker-connector-demo-june-2015Occ iware docker-connector-demo-june-2015
Occ iware docker-connector-demo-june-2015Fawaz Fernand PARAISO
 
Eclipse Mars News @JUG HH
Eclipse Mars News @JUG HHEclipse Mars News @JUG HH
Eclipse Mars News @JUG HHsimonscholz
 
Gretty: Managing Web Containers with Gradle
Gretty: Managing Web Containers with GradleGretty: Managing Web Containers with Gradle
Gretty: Managing Web Containers with GradleAndrey Hihlovsky
 
Continuously Break The Android
Continuously Break The AndroidContinuously Break The Android
Continuously Break The AndroidHasan Hosgel
 

Was ist angesagt? (10)

Angular 2 in-1
Angular 2 in-1 Angular 2 in-1
Angular 2 in-1
 
Eclipse Buildship JUG Hamburg
Eclipse Buildship JUG HamburgEclipse Buildship JUG Hamburg
Eclipse Buildship JUG Hamburg
 
Introduction to git flow
Introduction to git flowIntroduction to git flow
Introduction to git flow
 
Occ iware docker-connector-demo-june-2015
Occ iware docker-connector-demo-june-2015Occ iware docker-connector-demo-june-2015
Occ iware docker-connector-demo-june-2015
 
Git work flow
Git work flowGit work flow
Git work flow
 
Eclipse Mars News @JUG HH
Eclipse Mars News @JUG HHEclipse Mars News @JUG HH
Eclipse Mars News @JUG HH
 
git-flow R3Labs
git-flow R3Labsgit-flow R3Labs
git-flow R3Labs
 
Gradle by Example
Gradle by ExampleGradle by Example
Gradle by Example
 
Gretty: Managing Web Containers with Gradle
Gretty: Managing Web Containers with GradleGretty: Managing Web Containers with Gradle
Gretty: Managing Web Containers with Gradle
 
Continuously Break The Android
Continuously Break The AndroidContinuously Break The Android
Continuously Break The Android
 

Andere mochten auch

Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injectionSteve Ng
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injectionsrmelody
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015Nir Noy
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Androidgreenrobot
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksSlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 

Andere mochten auch (12)

Spring and dependency injection
Spring and dependency injectionSpring and dependency injection
Spring and dependency injection
 
Spring dependency injection
Spring dependency injectionSpring dependency injection
Spring dependency injection
 
Dagger 2
Dagger 2Dagger 2
Dagger 2
 
Groovy to gradle
Groovy to gradleGroovy to gradle
Groovy to gradle
 
Node.js code tracing
Node.js code tracingNode.js code tracing
Node.js code tracing
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Zookeeper
ZookeeperZookeeper
Zookeeper
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Ähnlich wie Implement DI in Java with Guice

スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化규영 허
 
Presentation on angular 5
Presentation on angular 5Presentation on angular 5
Presentation on angular 5Ramesh Adhikari
 
Writing testable Android apps
Writing testable Android appsWriting testable Android apps
Writing testable Android appsTomáš Kypta
 
BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...
BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...
BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...Mark A
 
Eclipse Buildship DemoCamp Hamburg (June 2015) with additional screenshots
Eclipse Buildship DemoCamp Hamburg (June 2015)  with additional screenshotsEclipse Buildship DemoCamp Hamburg (June 2015)  with additional screenshots
Eclipse Buildship DemoCamp Hamburg (June 2015) with additional screenshotssimonscholz
 
Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation ToolIzzet Mustafaiev
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIVisual Engineering
 
Gradle build automation tool
Gradle   build automation toolGradle   build automation tool
Gradle build automation toolIoan Eugen Stan
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testabilitydrewz lin
 
How do we test nodejs apps?
How do we test nodejs apps?How do we test nodejs apps?
How do we test nodejs apps?Michal Juhas
 
The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210Mahmoud Samir Fayed
 
Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...
Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...
Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...COMAQA.BY
 
The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196Mahmoud Samir Fayed
 
Cloud Study Jam Exploring the Cloud
Cloud Study Jam Exploring the CloudCloud Study Jam Exploring the Cloud
Cloud Study Jam Exploring the CloudProttay Karim
 
Unit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitUnit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitLars Thorup
 

Ähnlich wie Implement DI in Java with Guice (20)

Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
 
Presentation on angular 5
Presentation on angular 5Presentation on angular 5
Presentation on angular 5
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Dependency injection in iOS
Dependency injection in iOSDependency injection in iOS
Dependency injection in iOS
 
Writing testable Android apps
Writing testable Android appsWriting testable Android apps
Writing testable Android apps
 
BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...
BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...
BizSpark SF Lightning Talk: "Automated Testing (Unit, Integration and Systems...
 
Eclipse Buildship DemoCamp Hamburg (June 2015) with additional screenshots
Eclipse Buildship DemoCamp Hamburg (June 2015)  with additional screenshotsEclipse Buildship DemoCamp Hamburg (June 2015)  with additional screenshots
Eclipse Buildship DemoCamp Hamburg (June 2015) with additional screenshots
 
Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation Tool
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
 
Gradle build automation tool
Gradle   build automation toolGradle   build automation tool
Gradle build automation tool
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability
 
How do we test nodejs apps?
How do we test nodejs apps?How do we test nodejs apps?
How do we test nodejs apps?
 
The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210The Ring programming language version 1.9 book - Part 81 of 210
The Ring programming language version 1.9 book - Part 81 of 210
 
Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...
Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...
Дмитрий Лемешко. Comaqa Spring 2018. Continuous mobile automation in build pi...
 
The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196
 
Cloud Study Jam Exploring the Cloud
Cloud Study Jam Exploring the CloudCloud Study Jam Exploring the Cloud
Cloud Study Jam Exploring the Cloud
 
Unit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitUnit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnit
 
Gradle
GradleGradle
Gradle
 
Dagger2 Intro
Dagger2 IntroDagger2 Intro
Dagger2 Intro
 

Kürzlich hochgeladen

Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 

Kürzlich hochgeladen (20)

Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 

Implement DI in Java with Guice

  • 2. Plan ● TDD o A test from Guice’s user's guide (https://github.com/google/guice/wiki/Motivation) o A DI implementation to pass the test https://github.com/gdhuang/di_demo
  • 3. Dependency injection - A design pattern in which dependencies are injected into a dependent object Client.java public Client(Service1 s1, Service2 s2) Main.java Service1 s1 = new Service1Imp(); Service2 s2= new Service2Imp(); Client c = new Client(s1, s2);
  • 4. Guice ● Module - define bindings from types to implementations ● @Inject - specify a constructor is invoked by Guice
  • 6. Implementation ● Build a rule table of bindings ● Create an object respecting the bindings
  • 7. Rule table ● Module ● RuleBuilder ● Guice
  • 8. Injection ● resolve binding ● create an object using reflection
  • 9. Conclusion ● Java o Reflection http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/package-summary.html o Generic http://www.drdobbs.com/java-generics-and-c-templates/184401818 ● TDD o Add necessary tests before implementing furthur than existing tests o Practice with mockito http://stackoverflow.com/questions/7104485/how-does-jmock-and- mockito-create-wrappers