0x00 开始
开始以最无趣的 “Hello World” 开始吧。
1 | public class Hello { public String greeting(){ return "Hello World."; } } |
然后在 TextView 中显示,常规的方法是:
1 | Hello hello = new Hello(); textViewHello.setText(hello.greeting()); |
那么,如果使用 Dagger2 进行依赖注入,我们使用 @Inject 将 Hello 实例注入:
1 | @Inject Hello hello; ... textViewHello.setText(hello.greeting()); |
这里存在一个问题:要怎么实例化 Hello 呢? Dagger2 提供 @Inject 标注,在构造函数中标注后,当需要实例化时,Dagger2 就会调用这个构造函数:
1 | @Singleton public class Hello { @Inject public Hello() {} public String greeting(){ return "Hello World."; } } |
PS: 加入 Hello 也需要注入其他类呢? @Inject 同样轻松搞定,构造函数参数以及类成员属性均可轻松注入。
这时依然无法工作,如果你运行程序,会产生悲伤的 NullPointerException,那么还差什么呢?
我们需要创建一个Component,Component是Provider和Injector之间的桥梁。
1 | @Singleton @Component public interface DemoComponent { void inject(MainActivity activity); } |
Dagger2 会自动生成一个 DaggerDemoComponent,在 Activity 中调用注入:
1 | DaggerDemoComponent.create().inject(this); |
打完收工!
当然,你还需要通过 gradle 引入 Dagger2:
1 | ... apply plugin: 'com.neenbedankt.android-apt' buildscript { repositories { jcenter() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } } ... dependencies { ... provided 'javax.annotation:javax.annotation-api:1.2' apt 'com.google.dagger:dagger-compiler:2.0.2' compile 'com.google.dagger:dagger:2.0.2' } |
0x01 进阶
如上所述,我们自己写的代码可以通过 @Inject 实例化并注入,但若是第三方库提供的类呢?
这时需要用到 @Module, @Provide:
@Module
Module 是拥有可以提供依赖的函数的类,我们定义一个类,标注为 @Module,然后 Dagger2 就知道哪里可以找到构建一个对象所需要的依赖啦。而且 Module 被设计为可以被分割和组合的模式,有益于模块的划分。
@Provide
在标注为 @Module 的类中的函数可以标注为 @Provide, 标示可以提供相应的依赖,函数的前缀必须为 provides.
1 | @Module public class DemoModule { @Provides @Singleton public GithubService provideGithubService() { return new GithubService(); } } |
然后我们要告知 Component 使用这个 Module:
1 | @Singleton @Component(modules = {DemoModule.class}) public interface DemoComponent { void inject(MainActivity activity); } |
@Component 可以通过 dependencies 申明依赖依他的 @Component。
@Singleton
这是标示一个单例的注释,可以和 @Provide 共同标注一个函数,标示这个函数返回的对象都是单例的,也可以和 @Component 一起标注一个Component,标示这个 Component 的 Scope 是全局的。
@Scope
Scopes 非常的有用,Dagger2 可以通过自定义注解限定注解作用域。
下面我们定义一个生命周期和 Activity 一致的组件。
首先我们要申明一个 @Scope:
1 | @Scope @Retention(RetentionPolicy.RUNTIME) public @interface ActivityScope { } |
然后是 ActivityModule, 这个和前面 @Module 一样:
1 | @Module public class ActivityModule { private final Activity activity; public ActivityModule(Activity activity) { this.activity = activity; } @Provides @ActivityScope Activity activity() { return this.activity; } } |
接着声明 ActivityComponent 为 @ActivityScope:
1 | @ActivityScope @Component(dependencies = AppComponent.class, modules = ActivityModule.class) public interface ActivityComponent { inject(MainActivity activity); } |
在 Activity 中注入:
1 | @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActivityComponent().inject(this); } ... // 建议在基类 Activity 中声明方法 protected ActivityComponent getActivityComponent() { return DaggerActivityComponent.builder() .appComponent(DaggerAppComponent.create()) .activityModule(getActivityModule()) .build(); } protected ActivityModule getActivityModule() { return new ActivityModule(this); } |
最后 @Provides 方法或者可以直接注入的类中标注 @ActivityScope 即可让注入的实例与 Activity 声明周期保持一致。
@Qualifier
当类的类型不足以鉴别一个依赖的时候,我们就可以使用这个注解标示。
例如:在Android中,我们会需要不同类型的 Context,所以我们就可以定义 Qualifier 注解 @ForApplication 和 @ForActivity,这样当注入一个 Context 的时候,我们就可以告诉 Dagger 我们想要哪种类型的 Context。