SlideShare une entreprise Scribd logo
1  sur  15
Google Guice
Dependency Injection
VŨ CÔNG THÀNH
Tech Core team
Agenda
• Dependency Injection? YES/NO
• Google Guice Introduction
• Google Guice : HOW?
• Module
• Binding
• Scope
• Injection (Constructor, Field, and Method)
• Graph
• Demo
• Q & A
3
Dependency Injection
class SAMSUNG_S3() {
SAMSUNG_S3() {
ISpeaker s = new Speaker();
IMicro m = new Micro();
IScreen s = new Screen();
IKeyboard k = new Keyboard();
...
IHardDrive hd = new HardDrive();
IGraphicMemory gm = new GraphicMemory();
IGraphicCard g = new GraphicCard(gm);
ICPU c = new CPU();
//And finally!!!
samsungS3 = new Phone(s,m,g, c,…, ... ); }
}
Spagetti Code
4
Dependency Injection
class IPhone() {
IPhone() {
ISpeaker s = new Speaker();
IMicro m = new Micro();
IScreen s = new Screen();
IKeyboard k = new Keyboard();
...
IHardDrive hd = new HardDrive();
IGraphicMemory gm = new GraphicMemory();
IGraphicCard g = new GraphicCard(gm);
ICPU c = new CPU();
//And finally!!!
iphone = new Phone(s,m,g, c,…, ... ); }
}
How many “new” in your application?
Wild Programmer
5
Dependency Injection
6
Google Guice
• Guice is a dependency injection framework, which
alleviates the need for factories and use of new in your
java code.
• Google Guice implements the JSR-330
• Above Java 5 and brought to you by Google.
7
Google Guice
• Guice Module
• Binding
• Scope
• Injection (Constructor, Field, and Method)
• Graph
8
Guice Module
• Binding: Interface -> Implementation
public class RDBMSRewardBinder extends AbstractModule {
@Override
protected void configure() {
//Advertiser
bind(AdvertisersService.class).to(AdvertisersServiceImpl.class);
bind(AdvertisersStorage.class).to(AdvertisersJPAImpl.class);
bind(AdvertiserUsersService.class).to(AdvertiserUsersServiceImpl.class);
bind(AdvertiserUsersStorage.class).to(AdvertiserUsersJPAImpl.class);
//user
bind(UsersService.class).to(UsersServiceImpl.class);
bind(UserDevicesService.class).to(UserDevicesServiceImpl.class);
bind(UsersStorage.class).to(UsersJPAImpl.class);
bind(UserDevicesStorage.class).to(UserDevicesJPAImpl.class);
bind(UserStatusLogsStorage.class).to(UserStatusLogsJPAImpl.class);
bind(UserRefreshTokenService.class).to(UserRefreshTokenServiceImpl.class);
bind(UserRefreshTokensStorage.class).to(UserRefreshTokensJPAImpl.class);
...
9
Binding Types
• Multi Binding:
public class RequestLifecycleBinder1 extends AbstractModule {
@Override
protected void configure() {
Multibinder<ComponentRequestLifecycle> multibinder = Multibinder.newSetBinder(binder(), ComponentRequestLifecycle.class);
multibinder.addBinding().to(DBRequestLifecycle.class);
multibinder.addBinding().to(NoSQLRequestLifecycle.class);
}
}
class RequestLifecycleStack extends LinkedList<RequestLifecycle> {
@Inject
private Set<ComponentRequestLifecycle> allComponents;
10
Scope
• Unscoped: one per use
• create it, use it, and destroy it.
• Singleton: one per application
• For heavyweight resources
• and application state.
11
Scope
• Multi Binding
public class RequestLifecycleBinder1 extends AbstractModule {
@Override
protected void configure() {
Multibinder<ComponentRequestLifecycle> multibinder = Multibinder.newSetBinder(binder(), ComponentRequestLifecycle.class);
multibinder.addBinding().to(DBRequestLifecycle.class);
multibinder.addBinding().to(NoSQLRequestLifecycle.class);
}
}
class RequestLifecycleStack extends LinkedList<RequestLifecycle> {
@Inject
private Set<ComponentRequestLifecycle> allComponents;
12
Demo
• Step 0: Using ‘new’ in order to create the
RewardApp
• Step 1: Creates Module and Binding
• A module is a collection of bindings which is passed to Injector on its creation
• Step 2: Multi Binding
• Step 3: Binding with annotation and Singleton
scope
• Step 4: JsonModule and Binding from Json
13
Demo
• Step 5: Graph Objects
14
References
• Source Code:
• https://github.com/thanhvc/guice-demo
• https://github.com/google/guice/wiki/GettingStart
ed
Q&A

Contenu connexe

En vedette

Children young people and the arts_Arts Council England South West
Children young people and the arts_Arts Council England South WestChildren young people and the arts_Arts Council England South West
Children young people and the arts_Arts Council England South WestMartin Thomas
 
도박의세계『SX797』『СOM』카지노싸이트
도박의세계『SX797』『СOM』카지노싸이트도박의세계『SX797』『СOM』카지노싸이트
도박의세계『SX797』『СOM』카지노싸이트hdlkfjgldf
 
Katja Rajala liikuntatieteenpäivät 2015
Katja Rajala liikuntatieteenpäivät 2015Katja Rajala liikuntatieteenpäivät 2015
Katja Rajala liikuntatieteenpäivät 2015LIKESresearchcenter
 
Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...
Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...
Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...Ilya Slobodin
 
GLORY BEACH PACKAGES 2016
GLORY BEACH PACKAGES 2016GLORY BEACH PACKAGES 2016
GLORY BEACH PACKAGES 2016Jeevan Barath
 

En vedette (8)

Children young people and the arts_Arts Council England South West
Children young people and the arts_Arts Council England South WestChildren young people and the arts_Arts Council England South West
Children young people and the arts_Arts Council England South West
 
Final
FinalFinal
Final
 
M2 t1 planificador_aamtic.docx
M2 t1 planificador_aamtic.docxM2 t1 planificador_aamtic.docx
M2 t1 planificador_aamtic.docx
 
도박의세계『SX797』『СOM』카지노싸이트
도박의세계『SX797』『СOM』카지노싸이트도박의세계『SX797』『СOM』카지노싸이트
도박의세계『SX797』『СOM』카지노싸이트
 
ملخص البرمجة المرئية - الوحدة السادسة
ملخص البرمجة المرئية - الوحدة السادسةملخص البرمجة المرئية - الوحدة السادسة
ملخص البرمجة المرئية - الوحدة السادسة
 
Katja Rajala liikuntatieteenpäivät 2015
Katja Rajala liikuntatieteenpäivät 2015Katja Rajala liikuntatieteenpäivät 2015
Katja Rajala liikuntatieteenpäivät 2015
 
Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...
Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...
Собрать нельзя клонировать. Как выбрать подход к созданию кроссплатформенных ...
 
GLORY BEACH PACKAGES 2016
GLORY BEACH PACKAGES 2016GLORY BEACH PACKAGES 2016
GLORY BEACH PACKAGES 2016
 

Similaire à Eway google-guice presentation

Cocoa Heads Tricity - Design Patterns
Cocoa Heads Tricity - Design PatternsCocoa Heads Tricity - Design Patterns
Cocoa Heads Tricity - Design PatternsMaciej Burda
 
Easy path to machine learning
Easy path to machine learningEasy path to machine learning
Easy path to machine learningwesley chun
 
GEE Juli 2023.pptx
GEE Juli 2023.pptxGEE Juli 2023.pptx
GEE Juli 2023.pptxduabelaspkwu
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Jim Shingler
 
DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化Tomoharu ASAMI
 
GR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul KingGR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul KingGR8Conf
 
How To Build a Multi-Field Search Page For Your XPages Application
How To Build a Multi-Field Search Page For Your XPages ApplicationHow To Build a Multi-Field Search Page For Your XPages Application
How To Build a Multi-Field Search Page For Your XPages ApplicationMichael McGarel
 
SimpleModelerによるAndroidアプリ自動生成 with g3/g4
SimpleModelerによるAndroidアプリ自動生成 with g3/g4SimpleModelerによるAndroidアプリ自動生成 with g3/g4
SimpleModelerによるAndroidアプリ自動生成 with g3/g4Tomoharu ASAMI
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptwesley chun
 
Java onguice20070426
Java onguice20070426Java onguice20070426
Java onguice20070426Ratul Ray
 
Introduction to Gradio library in python.pptx
Introduction to Gradio library in python.pptxIntroduction to Gradio library in python.pptx
Introduction to Gradio library in python.pptxvahid67ebrahimian
 
Design Patterns para Microsserviços com MicroProfile
 Design Patterns para Microsserviços com MicroProfile Design Patterns para Microsserviços com MicroProfile
Design Patterns para Microsserviços com MicroProfileVíctor Leonel Orozco López
 
Up and Running with Angular
Up and Running with AngularUp and Running with Angular
Up and Running with AngularJustin James
 
Introduction to Grunt.js on Taiwan JavaScript Conference
Introduction to Grunt.js on Taiwan JavaScript ConferenceIntroduction to Grunt.js on Taiwan JavaScript Conference
Introduction to Grunt.js on Taiwan JavaScript ConferenceBo-Yi Wu
 
Grails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoGrails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoRubén Mondéjar Andreu
 

Similaire à Eway google-guice presentation (20)

OpenGL L03-Utilities
OpenGL L03-UtilitiesOpenGL L03-Utilities
OpenGL L03-Utilities
 
guice-servlet
guice-servletguice-servlet
guice-servlet
 
Cocoa Heads Tricity - Design Patterns
Cocoa Heads Tricity - Design PatternsCocoa Heads Tricity - Design Patterns
Cocoa Heads Tricity - Design Patterns
 
Easy path to machine learning
Easy path to machine learningEasy path to machine learning
Easy path to machine learning
 
GEE Juli 2023.pptx
GEE Juli 2023.pptxGEE Juli 2023.pptx
GEE Juli 2023.pptx
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106
 
DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化DIとトレイとによるAndroid開発の効率化
DIとトレイとによるAndroid開発の効率化
 
GR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul KingGR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul King
 
How To Build a Multi-Field Search Page For Your XPages Application
How To Build a Multi-Field Search Page For Your XPages ApplicationHow To Build a Multi-Field Search Page For Your XPages Application
How To Build a Multi-Field Search Page For Your XPages Application
 
Why Grails?
Why Grails?Why Grails?
Why Grails?
 
Why Grails
Why GrailsWhy Grails
Why Grails
 
SimpleModelerによるAndroidアプリ自動生成 with g3/g4
SimpleModelerによるAndroidアプリ自動生成 with g3/g4SimpleModelerによるAndroidアプリ自動生成 with g3/g4
SimpleModelerによるAndroidアプリ自動生成 with g3/g4
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScript
 
Java onguice20070426
Java onguice20070426Java onguice20070426
Java onguice20070426
 
Introduction to Gradio library in python.pptx
Introduction to Gradio library in python.pptxIntroduction to Gradio library in python.pptx
Introduction to Gradio library in python.pptx
 
Design Patterns para Microsserviços com MicroProfile
 Design Patterns para Microsserviços com MicroProfile Design Patterns para Microsserviços com MicroProfile
Design Patterns para Microsserviços com MicroProfile
 
Ashish resume
Ashish resumeAshish resume
Ashish resume
 
Up and Running with Angular
Up and Running with AngularUp and Running with Angular
Up and Running with Angular
 
Introduction to Grunt.js on Taiwan JavaScript Conference
Introduction to Grunt.js on Taiwan JavaScript ConferenceIntroduction to Grunt.js on Taiwan JavaScript Conference
Introduction to Grunt.js on Taiwan JavaScript Conference
 
Grails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoGrails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duo
 

Dernier

%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 

Dernier (20)

%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 

Eway google-guice presentation

  • 1. Google Guice Dependency Injection VŨ CÔNG THÀNH Tech Core team
  • 2. Agenda • Dependency Injection? YES/NO • Google Guice Introduction • Google Guice : HOW? • Module • Binding • Scope • Injection (Constructor, Field, and Method) • Graph • Demo • Q & A
  • 3. 3 Dependency Injection class SAMSUNG_S3() { SAMSUNG_S3() { ISpeaker s = new Speaker(); IMicro m = new Micro(); IScreen s = new Screen(); IKeyboard k = new Keyboard(); ... IHardDrive hd = new HardDrive(); IGraphicMemory gm = new GraphicMemory(); IGraphicCard g = new GraphicCard(gm); ICPU c = new CPU(); //And finally!!! samsungS3 = new Phone(s,m,g, c,…, ... ); } } Spagetti Code
  • 4. 4 Dependency Injection class IPhone() { IPhone() { ISpeaker s = new Speaker(); IMicro m = new Micro(); IScreen s = new Screen(); IKeyboard k = new Keyboard(); ... IHardDrive hd = new HardDrive(); IGraphicMemory gm = new GraphicMemory(); IGraphicCard g = new GraphicCard(gm); ICPU c = new CPU(); //And finally!!! iphone = new Phone(s,m,g, c,…, ... ); } } How many “new” in your application? Wild Programmer
  • 6. 6 Google Guice • Guice is a dependency injection framework, which alleviates the need for factories and use of new in your java code. • Google Guice implements the JSR-330 • Above Java 5 and brought to you by Google.
  • 7. 7 Google Guice • Guice Module • Binding • Scope • Injection (Constructor, Field, and Method) • Graph
  • 8. 8 Guice Module • Binding: Interface -> Implementation public class RDBMSRewardBinder extends AbstractModule { @Override protected void configure() { //Advertiser bind(AdvertisersService.class).to(AdvertisersServiceImpl.class); bind(AdvertisersStorage.class).to(AdvertisersJPAImpl.class); bind(AdvertiserUsersService.class).to(AdvertiserUsersServiceImpl.class); bind(AdvertiserUsersStorage.class).to(AdvertiserUsersJPAImpl.class); //user bind(UsersService.class).to(UsersServiceImpl.class); bind(UserDevicesService.class).to(UserDevicesServiceImpl.class); bind(UsersStorage.class).to(UsersJPAImpl.class); bind(UserDevicesStorage.class).to(UserDevicesJPAImpl.class); bind(UserStatusLogsStorage.class).to(UserStatusLogsJPAImpl.class); bind(UserRefreshTokenService.class).to(UserRefreshTokenServiceImpl.class); bind(UserRefreshTokensStorage.class).to(UserRefreshTokensJPAImpl.class); ...
  • 9. 9 Binding Types • Multi Binding: public class RequestLifecycleBinder1 extends AbstractModule { @Override protected void configure() { Multibinder<ComponentRequestLifecycle> multibinder = Multibinder.newSetBinder(binder(), ComponentRequestLifecycle.class); multibinder.addBinding().to(DBRequestLifecycle.class); multibinder.addBinding().to(NoSQLRequestLifecycle.class); } } class RequestLifecycleStack extends LinkedList<RequestLifecycle> { @Inject private Set<ComponentRequestLifecycle> allComponents;
  • 10. 10 Scope • Unscoped: one per use • create it, use it, and destroy it. • Singleton: one per application • For heavyweight resources • and application state.
  • 11. 11 Scope • Multi Binding public class RequestLifecycleBinder1 extends AbstractModule { @Override protected void configure() { Multibinder<ComponentRequestLifecycle> multibinder = Multibinder.newSetBinder(binder(), ComponentRequestLifecycle.class); multibinder.addBinding().to(DBRequestLifecycle.class); multibinder.addBinding().to(NoSQLRequestLifecycle.class); } } class RequestLifecycleStack extends LinkedList<RequestLifecycle> { @Inject private Set<ComponentRequestLifecycle> allComponents;
  • 12. 12 Demo • Step 0: Using ‘new’ in order to create the RewardApp • Step 1: Creates Module and Binding • A module is a collection of bindings which is passed to Injector on its creation • Step 2: Multi Binding • Step 3: Binding with annotation and Singleton scope • Step 4: JsonModule and Binding from Json
  • 13. 13 Demo • Step 5: Graph Objects
  • 14. 14 References • Source Code: • https://github.com/thanhvc/guice-demo • https://github.com/google/guice/wiki/GettingStart ed
  • 15. Q&A