eway google-guice presentation

15
Google Guice Dependency Injection VŨ CÔNG THÀNH Tech Core team

Upload: eway-joint-stock-company

Post on 23-Feb-2017

207 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Eway google-guice presentation

Google GuiceDependency Injection

VŨ CÔNG THÀNHTech Core team

Page 2: Eway google-guice presentation

Agenda

• Dependency Injection? YES/NO• Google Guice Introduction• Google Guice : HOW?

• Module• Binding• Scope• Injection (Constructor, Field, and Method)• Graph

• Demo• Q & A

Page 3: Eway google-guice presentation

Dependency Injectionclass 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

Page 4: Eway google-guice presentation

Dependency Injectionclass 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

Page 5: Eway google-guice presentation

Dependency Injection

Page 6: Eway google-guice presentation

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.

Page 7: Eway google-guice presentation

Google Guice

• Guice Module• Binding• Scope• Injection (Constructor, Field, and Method)• Graph

Page 8: Eway google-guice presentation

Guice Module

• Binding: Interface -> Implementationpublic class RDBMSRewardBinder extends AbstractModule {

@Overrideprotected void configure() {

//Advertiserbind(AdvertisersService.class).to(AdvertisersServiceImpl.class);bind(AdvertisersStorage.class).to(AdvertisersJPAImpl.class);bind(AdvertiserUsersService.class).to(AdvertiserUsersServiceImpl.class);bind(AdvertiserUsersStorage.class).to(AdvertiserUsersJPAImpl.class);

//userbind(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);

...

Page 9: Eway google-guice presentation

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;

Page 10: Eway google-guice presentation

Scope

• Unscoped: one per use• create it, use it, and destroy it.

• Singleton: one per application • For heavyweight resources• and application state.

Page 11: Eway google-guice presentation

Scope

• Multi Bindingpublic 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;

Page 12: Eway google-guice presentation

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

Page 13: Eway google-guice presentation

Demo

• Step 5: Graph Objects

Page 14: Eway google-guice presentation

References

• Source Code:• https://github.com/thanhvc/guice-demo• https://github.com/google/guice/wiki/Gett

ingStarted

Page 15: Eway google-guice presentation

Q&A