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

PDF
Guice - dependency injection framework
PDF
Nevera Bosch KGN36AW22
TXT
모바일스포츠북 ''SX797.COM'' 카지노노하우
TXT
Leeme importante
PDF
BSP the Arla Way 130416
TXT
최신작게임『SX797』『СOM』실시간바카라
PDF
Applied innovation battle mid term update
PDF
Exterior 27 kineo cove 1
Guice - dependency injection framework
Nevera Bosch KGN36AW22
모바일스포츠북 ''SX797.COM'' 카지노노하우
Leeme importante
BSP the Arla Way 130416
최신작게임『SX797』『СOM』실시간바카라
Applied innovation battle mid term update
Exterior 27 kineo cove 1

Viewers also liked (8)

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

Similar to Eway google-guice presentation (20)

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

Recently uploaded (20)

PDF
Topaz Photo AI Crack New Download (Latest 2025)
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PPTX
Airline CRS | Airline CRS Systems | CRS System
PDF
E-Commerce Website Development Companyin india
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PPTX
Download Adobe Photoshop Crack 2025 Free
PDF
Introduction to Ragic - #1 No Code Tool For Digitalizing Your Business Proces...
PDF
Visual explanation of Dijkstra's Algorithm using Python
PDF
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
PPTX
Lecture 5 Software Requirement Engineering
PDF
Microsoft Office 365 Crack Download Free
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PDF
MCP Security Tutorial - Beginner to Advanced
PPTX
Tech Workshop Escape Room Tech Workshop
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
DOC
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
PPTX
GSA Content Generator Crack (2025 Latest)
Topaz Photo AI Crack New Download (Latest 2025)
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Airline CRS | Airline CRS Systems | CRS System
E-Commerce Website Development Companyin india
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
Download Adobe Photoshop Crack 2025 Free
Introduction to Ragic - #1 No Code Tool For Digitalizing Your Business Proces...
Visual explanation of Dijkstra's Algorithm using Python
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
Lecture 5 Software Requirement Engineering
Microsoft Office 365 Crack Download Free
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
DNT Brochure 2025 – ISV Solutions @ D365
MCP Security Tutorial - Beginner to Advanced
Tech Workshop Escape Room Tech Workshop
Wondershare Recoverit Full Crack New Version (Latest 2025)
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
GSA Content Generator Crack (2025 Latest)

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