android 组件化dagger2,android dagger2使用心得

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

Scope

Scope 是用来确定注入的实例的生命周期的,如果没有使用 Scope 注解,Component 每次调用 Module 中的 provide 方法或 Inject 构造函数生成的工厂时都会创建一个新的实例,而使用 Scope 后可以复用之前的依赖实例。

在Dagger 2中

[email protected]

[email protected]生命周期一致。

[email protected]一致

scope可以给我们带来“局部单例”,生命周期取决于scope自己。

在 Dagger 2 官方文档中我找到一句话,[email protected]:

When a binding uses a scope annotation, that means that the component object holds a reference to the bound object until the component object itself is garbage-collected.

Scope 作用域的本质:Component 间接持有依赖实例的引用,把实例的作用域与 Component 绑定,它们不是同年同月同日生,但是同年同月死。

对于Android,我们通常会定义一个针对整个Activity的注解,[email protected]@Scope

@Documented

@Retention(RUNTIME)

public @interface ActivityScope {}更好的管理ApplicationComponent和Module之间的关系,Component和Component之间的依赖和继承关系。如果关系不匹配,在编译期间会报错,详细下面会介绍。

代码可读性,让程序猿更好的了解Module中创建的类实例的使用范围。Error:(13, 1) 错误: cn.xuexuan.newui.di.component.ActivityComponent (unscoped) may not reference scoped bindings:

@Singleton @Provides android.app.Activity cn.xuexuan.newui.di.module.ActivityModule.getActivity()

[email protected]@Component自身的scope不能相同,即dependencies组件之间的scope不能相同,否则出现下面错误

3、@Singleton的组件不能依赖其他scope的组件,[email protected]�现下面错误

4、没有scope的不能依赖有scope的组件。否则出现下面错误:Error:(21, 1) 错误: com.android.example.devsummit.archdemo.di.component.MyTestComponent (unscoped) cannot depend on scoped components:

@com.android.example.devsummit.archdemo.di.scope.ActivityScope com.android.example.devsummit.archdemo.di.component.MyTestComponentX

5、一个component不能同时有多个scope(Subcomponent除外),否则出现下面的错误Error:Execution failed for task ‘:app:compileDebugJavaWithJavac’.

java.lang.IllegalArgumentException: com.android.example.devsummit.archdemo.di.component.MyTestComponent was annotated with more than one @Scope annotation

@Binds

@Binds:可以理解为关联,[email protected],不同的在于@Provides 注解的方法都是有具体实现的,[email protected],并没有具体的实现的,在方法定义中方法参数必须是 返回值的实现类。这样创建实体类的地方就不用在Modules 中实现了,例如:@Binds

@Singleton

abstract AccountManagerDelegate accountManagerDelegate(AccountManagerDelegateImpl delegate);

Module 中不一定要具体实现,[email protected],这样在编译过程中会自动创建Fractory 以及实现的,AccountManagerDelegate中还可以使用该Module中 @Provides 提供的实体类

@BindsInstance

Component 可以在创建 Component 的时候绑定依赖实例,[email protected],只能在 Component.Builder 中使用。@Module

public final class HomeActivityModule {

private final HomeActivity activity;

public HomeActivityModule(HomeActivity activity) {

this.activity = activity;

}

@Provides

@ActivityScope // 自定义作用域

Activity provideActivity() {

return activity;

}

}@ActivityScope

@Component

public interface HomeActivityComponent {

@Component.Builder

interface Builder {

@BindsInstance

Builder activity(Activity activity);

HomeActivityComponent build();

}

}

注意在调用build()创建 Component 之前,[email protected] HomeActivityComponent 还可以注入 Activity 类型的依赖,但是不能注入 HomeActivity,因为 Dagger 2 是使用具体类型作为依据的([email protected] Activity [email protected] HomeActivity activity)。

注意:dagger.android 扩展库可以极大地简化在 Android 项目中使用 Dagger 2 的过程,但是还是有些限制,SubComponent.Builder 不能自定义 @BindsInstance 方法,SubCompoennt 的 Module 不能有含参数的构造函数,否则AndroidInjection.inject(this)在创建 SubComponent 时无法成功。

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值