Download - Android with dagger_2
![Page 1: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/1.jpg)
Android with Dagger 2⿈黃千碩 / Kros
iOS/Android Developer
![Page 2: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/2.jpg)
Dependency Injection
• 中⽂文 - 相依性注⼊入?
• No “new”, dependencies come to you.
![Page 3: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/3.jpg)
Dependency Injection
• ⼀一種 Design Pattern
• 最快速,且不⽤用任何 Liberary 即可達成
• 其實每個 app 中都已經有實作 DJ
![Page 4: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/4.jpg)
public class Tweeter { public void tweet(String tweet) { TwitterApi api = new TwitterApi(); api.postTweet("JakeWharton", tweet); }}
public class TwitterApi { public void postTweet(String user, String tweet) { OkHttpClient client = new OkHttpClient(); Request request = // TODO build POST request… client.newCall(request).execute(); }
Tweeter tweeter = new Tweeter(); tweeter.tweet("Hello, #Devoxx 2014!");
![Page 5: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/5.jpg)
public class TwitterApi { private final OkHttpClient client = new OkHttpClient();
public void postTweet(String user, String tweet) { Request request = // TODO build POST request client.newCall(request).execute(); }}
![Page 6: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/6.jpg)
public class TwitterApi { private final OkHttpClient client;
public TwitterApi(OkHttpClient client) { this.client = client; }
public void postTweet(String user, String tweet) { Request request = // TODO build POST request client.newCall(request).execute(); }}
![Page 7: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/7.jpg)
public class Tweeter { private final TwitterApi api = new TwitterApi(new OkHttpClient()); private final String user;
public Tweeter(String user) { this.user = user; }
public void tweet(String tweet) { api.postTweet(user, tweet); } }
![Page 8: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/8.jpg)
public class Tweeter { private final TwitterApi api = new TwitterApi(new OkHttpClient()); private final String user;
public Tweeter(String user) { this.user = user; }
public void tweet(String tweet) { api.postTweet(user, tweet); } }
Tweeter tweeter = new Tweeter("JakeWharton"); tweeter.tweet("Hello, #Devoxx 2014!"); tweeter.tweet("#Hungover #Dagger");
![Page 9: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/9.jpg)
Libraries
• Spring
• Guice
• Dagger (v1)
• PicoContainer
• CDI
![Page 10: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/10.jpg)
Guice• Developed at Google by Bob Lee, later Jesse
Wilson, others.
• Adopted and maintained by Java Core Libraries team. Powerful, dynamic, well-tested, wide-spread, etc...
• Configuration problems occur at runtime.
• Slow initialization, slow injection, memory concerns.
![Page 11: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/11.jpg)
Dagger (v1)• Developed at Square by Jesse Wilson advised by Bob
Lee.
• Initially targeted at highly resource constrained environments.
• Static analysis of all dependencies and injection points.
• Fail as early as possible (compile-time, not runtime)
• Eliminate reflection on methods, fields, and annotations.
![Page 12: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/12.jpg)
Dagger (v2)• Proposed and implemented by Java Core
Libraries team.
• Eliminate runtime library and generated code overhead.
• Shift remaining runtime analysis to compile time.
• Scoping with annotations and associated static analysis.
![Page 13: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/13.jpg)
Dagger API• @Module + @Provides: 提供相依性的機制
• @Inject: 要求提供相依性
• @Component: modules 與 injections 之間的橋樑,讓兩者能互通
• 另外有⼀一些語法糖可以使⽤用,使⽤用起來更⽅方便
• 被設計成可以拆成許多⼩小元件,並組合起來使⽤用
![Page 14: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/14.jpg)
Providing Dependencies
• Modules 是⼀一些 classes,⽽而這些 classes 中的 methods 提供相依性。
• 必須在 class 上加上 @Module
• 必須在每個 method 上加上 @Provides
![Page 15: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/15.jpg)
public class NetworkModule {
OkHttpClient provideOkHttpClient() { return new OkHttpClient(); }
TwitterApi provideTwitterApi(OkHttpClient client) { return new TwitterApi(client); }}
![Page 16: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/16.jpg)
@Module public class NetworkModule {
OkHttpClient provideOkHttpClient() { return new OkHttpClient(); }
TwitterApi provideTwitterApi(OkHttpClient client) { return new TwitterApi(client); }}
![Page 17: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/17.jpg)
@Module public class NetworkModule { !@Provides OkHttpClient provideOkHttpClient() { return new OkHttpClient(); }
@Provides TwitterApi provideTwitterApi(OkHttpClient client) { return new TwitterApi(client); }}
![Page 18: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/18.jpg)
@Module public class NetworkModule { !@Provides @Singleton OkHttpClient provideOkHttpClient() { return new OkHttpClient(); }
@Provides @Singleton TwitterApi provideTwitterApi(OkHttpClient client) { return new TwitterApi(client); }}
![Page 19: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/19.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
![Page 20: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/20.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
![Page 21: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/21.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
![Page 22: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/22.jpg)
Providing Dependencies
• Modules 是⼀一些 classes,⽽而這些 classes 中的 methods 提供相依性。
• 必須在 class 上加上 @Module
• 必須在每個 method 上加上 @Provides
• Modules 可以拆開成很多 module,也可以組合在⼀一起使⽤用
![Page 23: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/23.jpg)
@Modulepublic class TwitterModule { private final String user;
public TwitterModule(String user) { this.user = user; }
@Provides @Singleton Tweeter provideTweeter(TwitterApi api) { return new Tweeter(api, user); }
@Provides @Singleton Timeline provideTimeline(TwitterApi api) { return new Tweeter(api, user); } }
![Page 24: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/24.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
TwitterModule#provideTweeter
TwitterModule#provideTimeline
Tweeter
Timeline
![Page 25: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/25.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 26: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/26.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 27: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/27.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 28: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/28.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 29: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/29.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 30: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/30.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 31: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/31.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 32: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/32.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 33: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/33.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 34: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/34.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 35: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/35.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 36: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/36.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 37: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/37.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 38: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/38.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 39: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/39.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 40: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/40.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 41: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/41.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 42: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/42.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 43: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/43.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 44: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/44.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
TwitterModule#provideTweeter
TwitterModule#provideTimeline
Tweeter
Timeline
![Page 45: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/45.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 46: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/46.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
TwitterModule#provideTweeter
TwitterModule#provideTimeline
Tweeter
Timeline
![Page 47: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/47.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 48: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/48.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 49: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/49.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
TwitterModule#provideTweeter
TwitterModule#provideTimeline
Tweeter
Timeline
![Page 50: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/50.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 51: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/51.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 52: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/52.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
TwitterModule#provideTweeter
TwitterModule#provideTimeline
Tweeter
Timeline
![Page 53: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/53.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
Tweeter
Timeline
TwitterModule#provideTweeter
TwitterModule#provideTimeline
![Page 54: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/54.jpg)
OkHttpClient
Providing Dependencies
NetworkModule#provideOkHttpClient
TwitterApi NetworkModule#provideTwitterApi
TwitterModule#provideTweeter
TwitterModule#provideTimeline
Tweeter
Timeline
![Page 55: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/55.jpg)
Requesting Dependencies
• 必須要有 @Inject 的 annotation
• 有三種 inject ⽅方式
• Constructor, field, and method injection.
![Page 56: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/56.jpg)
Constructor Injection
• 在 Constructor 加上 @Inject
• 表⽰示 Constructor 的參數需要 dependency
• 這些參數可以被使⽤用在 private 或 final fields.
![Page 57: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/57.jpg)
public class TwitterApplication { private final Tweeter tweeter; private final Timeline timeline; @Inject public TwitterApplication(Tweeter tweeter, Timeline timeline) { this.tweeter = tweeter; this.timeline = timeline; } //… }
![Page 58: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/58.jpg)
Method Injection
• 在 methods 上加上 @Inject
• 表⽰示 method 的參數需要 dependency
• Injection 發⽣生在物件被完全建⽴立之後
• 只有⼀一個合理的 use case: passing ‘this’ to a dependency.
![Page 59: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/59.jpg)
public class TwitterApplication { private final Tweeter tweeter; private final Timeline timeline; @Inject public TwitterApplication(Tweeter tweeter, Timeline timeline) { this.tweeter = tweeter; this.timeline = timeline; } @Inject public void enableStreaming(Streaming streaming) { streaming.register(this); } }
![Page 60: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/60.jpg)
Field Injection
• 在 fields 上加上 @Inject
• Field 不能為 private 或是 final
• Injection 發⽣生在物件被完全建⽴立之後
• 在 android 中最常⾒見到
![Page 61: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/61.jpg)
public class TwitterApplication extends Activity { @Inject Tweeter tweeter; @Inject Timeline timeline; // ...}
![Page 62: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/62.jpg)
Components
• Modules 與 injections 之間的橋樑,讓兩者能互通
• Component 為 injector,實際發⽣生 inject 的⼈人
• Scope 的概念
![Page 63: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/63.jpg)
@Singleton @Component(modules = { NetworkModule.class, TwitterModule.class,!}) public interface TwitterComponent { }
![Page 64: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/64.jpg)
@Singleton @Component(modules = { NetworkModule.class, TwitterModule.class,!}) public interface TwitterComponent { Tweeter tweeter(); Timeline timeline(); }
![Page 65: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/65.jpg)
TwitterComponent component = DaggerTwitterComponent.builder() .twitterModule(new TwitterModule(“JakeWharton")) .build();
![Page 66: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/66.jpg)
TwitterComponent component = DaggerTwitterComponent.builder() .twitterModule(new TwitterModule(“JakeWharton")) .build();
@Modulepublic class TwitterModule { private final String user;
public TwitterModule(String user) { this.user = user; } // …
![Page 67: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/67.jpg)
TwitterComponent component = DaggerTwitterComponent.builder() .twitterModule(new TwitterModule(“JakeWharton")) .build();
Tweeter tweeter = component.tweeter(); tweeter.tweet("Hello, #Devoxx 2014!");
![Page 68: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/68.jpg)
public class TwitterApplication implements Runnable { private final Tweeter tweeter; private final Timeline timeline;
@Inject public TwitterApplication(Tweeter tweeter, Timeline timeline) { this.tweeter = tweeter; this.timeline = timeline;}
@Override public void run() { tweeter.tweet("Hello #Devoxx 2014!”);
timeline.loadMore(20); for (Tweet tweet : timeline.get()) { System.out.println(tweet); } } }
![Page 69: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/69.jpg)
@Singleton @Component(modules = { NetworkModule.class, TwitterModule.class,!}) public interface TwitterComponent { TwitterApplication app(); }
![Page 70: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/70.jpg)
@Singleton @Component(modules = { NetworkModule.class, TwitterModule.class,!}) public interface TwitterComponent { TwitterApplication app(); }
TwitterComponent component = DaggerTwitterComponent.builder() .twitterModule(newTwitterModule(“JakeWharton")) .build();
component.app().run();
![Page 71: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/71.jpg)
public class TwitterApplication implements Runnable { @Inject Tweeter tweeter; @Inject Timeline timeline;
@Inject public TwitterApplication(Tweeter tweeter, Timeline timeline) { this.tweeter = tweeter; this.timeline = timeline;}
@Override public void run() { tweeter.tweet("Hello #Devoxx 2014!”);
timeline.loadMore(20); for (Tweet tweet : timeline.get()) { System.out.println(tweet); } } }
![Page 72: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/72.jpg)
@Singleton @Component(modules = { NetworkModule.class, TwitterModule.class,!}) public interface TwitterComponent { void injectApp(TwitterApplication app); }
![Page 73: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/73.jpg)
@Singleton @Component(modules = { NetworkModule.class, TwitterModule.class,!}) public interface TwitterComponent { void injectApp(TwitterApplication app); }
TwitterComponent component = DaggerTwitterComponent.builder() .twitterModule(newTwitterModule(“JakeWharton")) .build();
TwitterApplication app = new TwitterApplication(); component.injectApp(app); app.run();
![Page 74: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/74.jpg)
@Singleton @Component(modules = { NetworkModule.class, TwitterModule.class,!}) public interface TwitterComponent { void injectActivity(TwitterActivity activity); }
TwitterComponent component = DaggerTwitterComponent.builder() .twitterModule(newTwitterModule(“JakeWharton")) .build();
TwitterActivity activity = // Android creates instance...component.injectActivity(activity); // use tweet and timeline in activity…
![Page 75: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/75.jpg)
Components
• Modules 與 injections 之間的橋樑,讓兩者能互通
• Component 為 injector,實際發⽣生 inject 的⼈人
• Scope 的概念
![Page 76: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/76.jpg)
@Component( dependencies = ApiComponent.class, modules = TwitterModule.class)public interface TwitterComponent { void injectActivity(TwitterActivity activity); }
@Singleton@Component(modules = NetworkModule.class)public interface ApiComponent { }
![Page 77: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/77.jpg)
@Singleton@Component(modules = NetworkModule.class)public interface ApiComponent { TwitterApi api();}
@Component( dependencies = ApiComponent.class, modules = TwitterModule.class)public interface TwitterComponent { void injectActivity(TwitterActivity activity); }
![Page 78: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/78.jpg)
@Modulepublic class TwitterModule { private final String user;
public TwitterModule(String user) { this.user = user; }
@Provides @Singleton Tweeter provideTweeter(TwitterApi api) { return new Tweeter(api, user); }
@Provides @Singleton Timeline provideTimeline(TwitterApi api) { return new Tweeter(api, user); } }
![Page 79: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/79.jpg)
ApiComponent apiComponent = Dagger_ApiComponent.create();
TwitterComponent twitterComponent = DaggerTwitterComponent.builder() .apiComponent(apiComponent).twitterModule(new TwitterModule("JakeWharton")).build();
TwitterActivity activity = // Android creates instance...component.injectActivity(activity);
![Page 80: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/80.jpg)
Scope Annotations
• Only create a single instance.
• @Singleton 是最⼤大的 scope (可以想像是最上層的 scope)
• ⾃自定 scope,可以讓程式更清楚,也可以有較短的 lifecycle
@Scopepublic @interface User {}
![Page 81: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/81.jpg)
@Modulepublic class TwitterModule { private final String user;
public TwitterModule(String user) { this.user = user; }
@Provides Tweeter provideTweeter(TwitterApi api) { return new Tweeter(api, user); }
@Provides Timeline provideTimeline(TwitterApi api) { return new Tweeter(api, user); } }
![Page 82: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/82.jpg)
@Modulepublic class TwitterModule { private final String user;
public TwitterModule(String user) { this.user = user; }
@Provides @User Tweeter provideTweeter(TwitterApi api) { return new Tweeter(api, user); }
@Provides @User Timeline provideTimeline(TwitterApi api) { return new Tweeter(api, user); } }
![Page 83: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/83.jpg)
@Component( dependencies = ApiComponent.class, modules = TwitterModule.class)public interface TwitterComponent { void injectActivity(TwitterActivity activity); }
![Page 84: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/84.jpg)
@User @Component( dependencies = ApiComponent.class, modules = TwitterModule.class)public interface TwitterComponent { void injectActivity(TwitterActivity activity); }
![Page 85: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/85.jpg)
![Page 86: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/86.jpg)
![Page 87: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/87.jpg)
![Page 88: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/88.jpg)
![Page 89: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/89.jpg)
![Page 90: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/90.jpg)
![Page 91: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/91.jpg)
![Page 92: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/92.jpg)
![Page 93: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/93.jpg)
![Page 94: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/94.jpg)
![Page 95: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/95.jpg)
![Page 96: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/96.jpg)
Dagger 1
public abstract class ObjectGraph { public static ObjectGraph create(Object... modules) {} public abstract ObjectGraph plus(Object... modules); public abstract void validate(); public abstract void injectStatics(); public abstract <T> T get(Class<T> type); public abstract <T> T inject(T instance);}
public @interface Module { Class<?>[] injects() default { }; Class<?>[] staticInjections() default { }; Class<?>[] includes() default { }; Class<?> addsTo() default Void.class; boolean overrides() default false; boolean complete() default true; boolean library() default true;}
public @interface Provides { enum Type { UNIQUE, SET } Type type() default Type.UNIQUE;}
public interface Lazy<T> { T get();}public interface MembersInjector<T> { void injectMembers(T instance);}
![Page 97: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/97.jpg)
Dagger 2
public @interface Component { Class<?>[] modules() default {}; Class<?>[] dependencies()default {}; } public @interface Module {
Class<?>[] includes() default {};}
public @interface Provides {}
public @interface MapKey { boolean unwrapValue();}
public interface Lazy<T> { T get(); }
![Page 98: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/98.jpg)
Issues
• Gained 13% of processor performance at google scale
• No reflection at all
• Less flexible
![Page 100: Android with dagger_2](https://reader030.vdocuments.pub/reader030/viewer/2022032616/55a549d51a28ab3c198b46dc/html5/thumbnails/100.jpg)
Reference• DAGGER 2 - A New Type of dependency injection
https://www.youtube.com/watch?v=oK_XtfXPkqw
• Dependency Injection with Dagger 2 https://speakerdeck.com/jakewharton/dependency-injection-with-dagger-2-devoxx-2014
• The Future of Dependency Injection with Dagger 2 https://www.parleys.com/tutorial/5471cdd1e4b065ebcfa1d557/