Dagger2

Dagger2

@Inject

Declaring Dependencies

  • Use @Inject to annotate the constructor that Dagger should use to create instances of a class. When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.

    class Thermosiphon implements Pump {
      private final Heater heater;
    
      @Inject
      Thermosiphon(Heater heater) {
        this.heater = heater;
      }
      ...
    }
    
  • Dagger can inject fields directly. In this example it obtains a Heater instance for the heater field and a Pump instance for the pump field.

    class CoffeeMaker {
      @Inject Heater heater;
      @Inject Pump pump;
      ...
    }
    
  • If your class has @Inject-annotated fields but no @Inject-annotated constructor, Dagger will inject those fields if requested, but will not create new instances. Add a no-argument constructor with the @Inject annotation to indicate that Dagger may create instances as well.

    Dagger also supports method injection, though constructor or field injection are typically preferred.

    Classes that lack @Inject annotations cannot be constructed by Dagger.


Satisfying Dependencies(满足依赖)

By default, Dagger satisfies each dependency by constructing an instance of the requested type as described above. When you request a CoffeeMaker, it’ll obtain one by calling new CoffeeMaker() and setting its injectable fields.

But @Inject doesn’t work everywhere:

  • Interfaces can’t be constructed.(接口)
  • Third-party classes can’t be annotated.(第三方库)
  • Configurable objects must be configured!(可配置的对象?)

@Provide

For these cases where @Inject is insufficient or awkward, use an @Provides-annotated method to satisfy a dependency. The method’s return type defines which dependency it satisfies.

For example, provideHeater() is invoked whenever a Heater is required:

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

It’s possible for @Provides methods to have dependencies of their own. This one returns a Thermosiphon whenever a Pump is required:(自己也可以通过@Provides对外提供实例)

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

@Module

All @Provides methods must belong to a module. These are just classes that have an @Module annotation.

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

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

By convention, @Provides methods are named with a provide prefix and module classes are named with a Module suffix.

@Component

The @Inject and @Provides-annotated classes form a graph of objects, linked by their dependencies. Calling code like an application’s mainmethod or an Android Application accesses that graph via a well-defined set of roots.

In Dagger 2, that set is defined by an interface with methods that have no arguments and return the desired type. By applying the @Component annotation to such an interface and passing the module types to the modules parameter, Dagger 2 then fully generates an implementation of that contract.

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

The implementation has the same name as the interface prefixed with Dagger. Obtain an instance by invoking the builder() method on that implementation and use the returned builder to set dependencies and build() a new instance.

CoffeeShop coffeeShop = DaggerCoffeeShop.builder()
    .dripCoffeeModule(new DripCoffeeModule())
    .build();

Note: If your @Component is not a top-level type, the generated component’s name will be include its enclosing types’ names, joined with an underscore. For example, this code:

class Foo {
  static class Bar {
    @Component
    interface BazComponent {}
  }
}

would generate a component named DaggerFoo_Bar_BazComponent.


Any module with an accessible default constructor can be elided as the builder will construct an instance automatically if none is set. And for any module whose @Provides methods are all static, the implementation doesn’t need an instance at all. If all dependencies can be constructed without the user creating a dependency instance, then the generated implementation will also have a create() method that can be used to get a new instance without having to deal with the builder.

CoffeeShop coffeeShop = DaggerCoffeeShop.create();

//create方法等价于Builder().build()
create() {
    return new Builder().build();
}

Now, our CoffeeApp can simply use the Dagger-generated implementation of CoffeeShop to get a fully-injected CoffeeMaker.

public class CoffeeApp {
  public static void main(String[] args) {
    CoffeeShop coffeeShop = DaggerCoffeeShop.create();
    coffeeShop.maker().brew();
  }
}

@Singletons

保证在一个Component是单例,多个Component中还是多个对象,注意Component也需要用Singletons声明。

//实现层面使用了DoubleCheck将Module进行装饰
@Module
public class FoodModule {

    @Singleton
    @Provides
    Chopsticks provideChopsticks() {
        return new Chopsticks();
    }
}

@Singleton
@Component(modules = FoodModule.class)
public interface FoodComponent {
    Person injectPerson();
}

//返回的是一个DoubleCheck对象
this.provideChopsticksProvider =
        DoubleCheck.provider(FoodModule_ProvideChopsticksFactory.create(builder.foodModule));

//获取实例,走的是DoubleCheck的get方法
instance.chopsticks = chopsticksAndChopsticks2Provider.get();

DoubleCheck

public final class DoubleCheck<T> implements Provider<T>, Lazy<T> {
  private static final Object UNINITIALIZED = new Object();

  private volatile Provider<T> provider;
  private volatile Object instance = UNINITIALIZED;

  private DoubleCheck(Provider<T> provider) {
    assert provider != null;
    this.provider = provider;
  }

  @SuppressWarnings("unchecked") // cast only happens when result comes from the provider
  @Override
  public T get() {
    Object result = instance;
    if (result == UNINITIALIZED) {
      synchronized (this) {
        result = instance;
        if (result == UNINITIALIZED) {
          result = provider.get();
          /* Get the current instance and test to see if the call to provider.get() has resulted
           * in a recursive call.  If it returns the same instance, we'll allow it, but if the
           * instances differ, throw. */
          Object currentInstance = instance;
          if (currentInstance != UNINITIALIZED && currentInstance != result) {
            throw new IllegalStateException("Scoped provider was invoked recursively returning "
                + "different results: " + currentInstance + " & " + result + ". This is likely "
                + "due to a circular dependency.");
          }
          instance = result;
          /* Null out the reference to the provider. We are never going to need it again, so we
           * can make it eligible for GC. */
          provider = null;
        }
      }
    }
    return (T) result;
  }

  /** Returns a {@link Provider} that caches the value from the given delegate provider. */
  public static <T> Provider<T> provider(Provider<T> delegate) {
    checkNotNull(delegate);
    if (delegate instanceof DoubleCheck) {
      /* This should be a rare case, but if we have a scoped @Binds that delegates to a scoped
       * binding, we shouldn't cache the value again. */
      return delegate;
    }
    return new DoubleCheck<T>(delegate);
  }
}

@Scope

Dagger2的Scope,除了Singleton(root),其他都是自定义的,无论你给它命名PerActivity、PerFragment,其实都只是一个命名而已,真正起作用的是inject的位置,以及dependency。

Scope起的更多是一个限制作用,比如不同层级的Component需要有不同的scope,注入PerActivity scope的component后activity就不能通过@Inject去获得SingleTon的实例,需要从application去暴露接口获得(getAppliationComponent获得component实例然后访问,比如全局的navigator)。

当然,另一方面则是可读性和方便理解,通过scope的不同很容易能辨明2个实例的作用域的区别。

@Qualifier

用于不同的构造方法标识,替代@Name

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface FoodForDefault {
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface FoodForDefault {
}

@Module
public class FoodModule {

    @FoodForDefault
    @Provides
    Food provideFood() {
        return new Food();
    }

    @FoodForSomeThing
    @Provides
    Food provideFoodForSomeThing() {
      //不支持带参数,如果我需要的参数是可变的,如何操作?
        return new Food("哈哈哈");
    }
}

public class Person {

    @FoodForDefault
    @Inject
    Food food;

    @FoodForSomeThing
    @Inject
    Food food1;
}

在实现层面上@Qualifier标示后就再生成了一个Factory用于构造对象

Other

依赖注入的过程

步骤1:查找Module中是否存在创建该类的方法。
步骤2:若存在创建类方法,查看该方法是否存在参数
    步骤2.1:若存在参数,则按从**步骤1**开始依次初始化每个参数
    步骤2.2:若不存在参数,则直接初始化该类实例,一次依赖注入到此结束
步骤3:若不存在创建类方法,则查找Inject注解的构造函数,
           看构造函数是否存在参数
    步骤3.1:若存在参数,则从**步骤1**开始依次初始化每个参数
    步骤3.2:若不存在参数,则直接初始化该类实例,一次依赖注入到此结束

硬初始化or硬编码

public class CoffeeMachine {
    private CoffeeMaker maker;
    public CoffeeMachine(Cooker cooker){
        maker = new SimpleMaker(cooker);
    }
    public String makeCoffee(){
        return maker.makeCoffee();
    }
}

解决上述问题的方式

public class CoffeeMachinWithInjection implements InjectMaker{
    private CoffeeMaker maker;
    /*依赖注入的3种常见形式
     *No.1  构造函数注入
     */
    public CoffeeMachinWithInjection(CoffeeMaker maker){
        this.maker = maker;
    }
    //No.2  Setter注入
    public void setMaker(CoffeeMaker maker){
        this.maker = maker;
    }
    // //No.3 接口注入
    @Override
    public void injectMaker(CoffeeMaker maker) {
        this.maker = maker;
    }
    public String makeCoffee(){
        return maker.makeCoffee();
    }
}

参考

http://blog.zhaiyifan.cn/2016/03/27/android-new-project-from-0-p4/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容