SlideShare a Scribd company logo
1 of 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

More Related Content

Viewers also liked

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
 

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

Recently uploaded

%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
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
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 - 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
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...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
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%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
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
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
 

Recently uploaded (20)

%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
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
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 - 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...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%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
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%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
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
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
 

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