Eway google-guice presentation
-
Upload
eway-joint-stock-company -
Category
Software
-
view
204 -
download
0
Transcript of Eway google-guice presentation
Google GuiceDependency Injection
VŨ CÔNG THÀNHTech 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
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
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
Dependency Injection
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.
Google Guice
• Guice Module• Binding• Scope• Injection (Constructor, Field, and Method)• Graph
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);
...
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;
Scope
• Unscoped: one per use• create it, use it, and destroy it.
• Singleton: one per application • For heavyweight resources• and application state.
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;
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
Demo
• Step 5: Graph Objects
References
• Source Code:• https://github.com/thanhvc/guice-demo• https://github.com/google/guice/wiki/Gett
ingStarted
Q&A