Android依赖注入之Dagger2注解介绍

gradle导入

这个配置和AndroidAnnotation 配置相似,都需要配置apt 插件

  • 首先打开项目的 build.gradle,添加如下代码,可能apt 插件会有升级
dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
  • 打开Module 中的build.gradle,添加如下代码
apply plugin: 'com.neenbedankt.android-apt'

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.bobomee.dagger2demo'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'

    apt 'com.google.dagger:dagger-compiler:2.0'
    compile 'com.google.dagger:dagger:2.0'
    provided 'javax.annotation:jsr250-api:1.0'
}

Dagger2 注解

@Inject

在需要依赖的地方使用这个注解,告诉Dagger这个类或者字段需要依赖注入

class Thermosiphon implements Pump {
  private final Heater heater;

  @Inject
  Thermosiphon(Heater heater) {
    this.heater = heater;
  }

  ...
}
class CoffeeMaker {
  @Inject Heater heater;
  @Inject Pump pump;

  ...
}

@Provides

在@Module 中,我们定义的方法用这个注解,用于告诉 Dagger 我们需要构造实例并提供依赖.所有的Provide方法必须属于Module

@Provides Heater provideHeater() {
  return new ElectricHeater();
}

@Module

用于专门提供依赖,里面提供了一系列provide方法,用于告诉Dagger 去哪里找到这些依赖

@Module
class DripCoffeeModule {
  @Provides Heater provideHeater() {
    return new ElectricHeater();
  }

  @Provides Pump providePump(Thermosiphon pump) {
    return pump;
  }
}

@Component

是@Inject和@Module的桥梁,需要列出所有的@Modules以组成该组件.In Dagger 2, that set is defined by an interface ,这是和Dagger1 有区别的

@Component(modules = DripCoffeeModule.class)
interface CoffeeShop {
  CoffeeMaker maker();
}

@Scope

注解作用域,通过自定义注解限定对象的作用范围,(如@PerActivity自定义注解,限定对象的存活时间和Activity一致)

Since Dagger 2 associates scoped instances in the graph with instances of component implementations, the components themselves need to declare which scope they intend to represent
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface PerActivity {
}
@PerActivity
@Component(dependencies = ActivityComponent.class, modules = ContainerModule.class)
public interface ContainerComponent {
    void inject(MainActivity mainActivity);
}

@Singleton

单例,使用@Singleton注解之后,对象只会被初始化一次,之后的每次都会被直接注入相同的对象,@Singleton 就是一个内置的作用域

@Provides @Singleton Heater provideHeater() {
  return new ElectricHeater();
}

@Qualifier

限定符,当@Inject不能通过类的类型来鉴别一个 依赖的时候,会使用到(如@ForApplication 和 @ForActivity,分别代表 Application和Activity的Context)

@Qualifier
@Documented
@Retention(RUNTIME)
public @interface Named {
  String value() default "";
}
class ExpensiveCoffeeMaker {
  @Inject @Named("water") Heater waterHeater;
  @Inject @Named("hot plate") Heater hotPlateHeater;
  ...
}

Lazy injections

懒加载:

Sometimes you need an object to be instantiated lazily. For any binding T, you can create a Lazy<T> which defers instantiation until the first call to Lazy<T>'s get() method. 
class GridingCoffeeMaker {
  @Inject Lazy<Grinder> lazyGrinder;

 ...
}

参考:
Dagger2 官方文档
使用Dagger 2进行依赖注入
详解Dagger2翻译自:Tasting Dagger 2 on Android

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值