Android Dagger2(基础篇)

1,198 阅读5分钟

对Dagger2从入门到放弃再到恍然大悟。
第一次接触这个框架的时候,在网上看了十几篇文章。看完之后的感受是,二脸懵逼,完全不晓得写的是什么。
然后过了一段时间,又尝试着去了解,并且一边照着别人家的demo依葫芦画瓢,但是后来发现,写不下去,我不晓得我这些代码写的是什么意思,然后有扔一边了。
第三次,又心血来潮,静下心来硬着头皮反复看,突然有一丝说是灵感?思路?猜测?立马抓住,然后整条线立马完整的串起来了。


添加依赖

//project
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
//app
apply plugin: 'com.neenbedankt.android-apt'
apt 'com.google.dagger:dagger-compiler:2.5'
compile 'com.google.dagger:dagger:2.5'
provided 'org.glassfish:javax.annotation:10.0-b28'

如果要和ButterKnife一起用

packagingOptions {    
    exclude 'META-INF/services/javax.annotation.processing.Processor'
}

//apt改为provided
//apt 'com.google.dagger:dagger-compiler:2.5'
provided 'com.google.dagger:dagger-compiler:2.5'

我发现我之前看这个框架一直没看懂,有一个原因是太局限于代码。就是说,框架的使用是为了让我们打代码更简单更容易,而不是为了逼格高(当然逼格确实会变高=。=但是这不是主要目的)从最原始的角度出发,框架都是用来达成某种目的的。Dagger2的目的是依赖注入,说白一点,就是实例化对象,说清楚一点,就是你叫Dagger2帮你实例化对象,这就是最直接的目的,所有的Class和Annotation都是为了达到这个目的辅助工具,所以不必去纠结这些。下面先举个例子。。。


比如现在有Fruit,SaladDressing,FruitSalad三个类,水果,沙拉酱,水果沙拉

public class FruitSalad {    
    private SaladDressing saladDressing;    
    private Fruit fruit;    

    @Inject
    public FruitSalad(SaladDressing saladDressing, Fruit fruit) {        
        this.saladDressing = saladDressing;        
        this.fruit = fruit;    
    }    
}

public class SaladDressing {
}

public class Fruit {
}

好了现在我们自己做一份FruitSalad

FruitSalad fruitSalad = new FruitSalad(new SaladDressing(), new Fruit());

但是我不想自己做,我需要Dagger2帮我做一份。当然FruitSalad(水果沙拉)需要SaladDressing(沙拉酱)和Fruit(水果)才能做,所以SaladDressing(沙拉酱)和Fruit(水果)我依旧自己提供。
不过Dagger2也有自己的规则,如果想要帮忙做FruitSalad(水果沙拉),那么你不但需要提供SaladDressing(沙拉酱)和Fruit(水果)的实例,还要把SaladDressing(沙拉酱)和Fruit(水果)放到Module中。
或者说,你们需要约定一个地方,你把SaladDressing(沙拉酱)和Fruit(水果)放到那个地方,Dagger2从那个地方去取SaladDressing(沙拉酱)和Fruit(水果),用来做FruitSalad(水果沙拉),那个地方就是Module。

@Module
public class FruitSaladModule {    
    private final SaladDressing saladDressing;    
    private final Fruit fruit;    

    public FruitSaladModule(SaladDressing saladDressing, Fruit fruit) {        
        this.saladDressing = saladDressing;        
        this.fruit = fruit;    
    }    

    //提供沙拉酱
    @FruitSaladScope    
    @Provides    
    SaladDressing provideSaladDressing() {        
        return saladDressing;    
    }    

    //提供水果
    @FruitSaladScope    
    @Provides    
    Fruit provideFruit() {        
        return fruit;    
    }
}

使用@Module表明这是一个Module
使用@Provides表明提供这些依赖
现在我把Module准备好了,那我怎么让Dagger2帮我做FruitSalad(水果沙拉)呢?这个时候就需要Component了

@FruitSaladScope
@Component(modules = FruitSaladModule.class)
public interface FruitSaladComponent {    
    void inject(MainActivity activity);
}

FruitSaladComponent告诉了Dagger2做FruitSalad(水果沙拉)需要的Module是FruitSaladModule,那里有SaladDressing(沙拉酱)和Fruit(水果)可以拿。需要注入到MainActivity,我需要在MainActivity拿到FruitSalad。
使用@Component表示这是一个Component
modules={}表示可以用哪些Module去拿需要的依赖

DaggerFruitSaladComponent.builder()        
    .fruitSaladModule(new FruitSaladModule(new SaladDressing(), new Fruit()))        
    .build().inject(this);

接下来你只要把Module交给Dagger2,Dagger2就会帮你做FruitSalad(水果沙拉)了,而且SaladDressing(沙拉酱)和Fruit(水果)都是我自己提供的。
然后现在有人和我说,你的SaladDressing(沙拉酱)我包了,所以我就只需要提供Fruit(水果)就够了,而且我自己不做,那SaladDressing(沙拉酱)就让Dagger2帮我保管,我只要告诉他SaladDressing(沙拉酱)去哪里拿就好了。

@Module
public class SaladDressingModule {    
    private final SaladDressing saladDressing;   

    public SaladDressingModule(SaladDressing saladDressing) {        
        this.saladDressing = saladDressing;    
    }    

    @SaladDressingScope    
    @Provides    
    SaladDressing provideSaladDressing() {        
        return saladDressing;    
    }
}

@SaladDressingScope
@Component(modules = SaladDressingModule.class)
public interface SaladDressingComponent {    
    SaladDressing getSaladDressing();
}

所以现在我把SaladDressing(沙拉酱)放到了SaladDressingModule里,Dagger2用SaladDressingComponent就可以拿到了

SaladDressingComponent saladDressingComponent = DaggerSaladDressingComponent.builder()        
    .saladDressingModule(new SaladDressingModule(别人给的saladDressing))
    .build();

既然我不需要自己提供SaladDressing(沙拉酱)那么FruitSaladModule和FruitSaladComponent应该修改一下

@Module
public class FruitSaladModule { 
    private final Fruit fruit; 

    public FruitSaladModule(Fruit fruit) { 
        this.fruit = fruit; 
    } 

    //提供水果 
    @FruitSaladScope 
    @Provides 
    Fruit provideFruit() { 
        return fruit; 
    }
}

@FruitSaladScope
@Component(dependencies = SaladDressingComponent.class, modules = FruitSaladModule.class)
public interface FruitSaladComponent { 
    void inject(MainActivity activity);
}

这样,SaladDressing(沙拉酱)我交给Dagger2保管,通过SaladDressingComponent就可以拿到,Fruit(水果)依旧会提供给FruitSaladModule

DaggerFruitSaladComponent.builder()        
    .saladDressingComponent(saladDressingComponent)        
    .fruitSaladModule(new FruitSaladModule(new Fruit()))        
    .build().inject(this);

dependencies={}告诉Dagger2可以去别的Component找需要的依赖,把代码补全差不多这样

@Inject
FruitSalad fruitSalad;

SaladDressingComponent saladDressingComponent = DaggerSaladDressingComponent.builder() 
    .saladDressingModule(new SaladDressingModule(别人给的saladDressing)) 
    .build();

DaggerFruitSaladComponent.builder()        
    .saladDressingComponent(saladDressingComponent)        
    .fruitSaladModule(new FruitSaladModule(new Fruit()))        
    .build().inject(this);

现在我又想要VegetableSalad(蔬菜沙拉)了,继续让Dagger2帮我做,反正SaladDressing(沙拉酱)已经有了,我只要提供蔬菜就好了

@Module
public class VegetableSaladModule { 
    private final Vegetable vegetable; 

    public VegetableSaladModule(Vegetable vegetable) { 
        this.vegetable = vegetable; } 

    //提供蔬菜  
    @VegetableSaladScope 
    @Provides 
    Vegetable provideVegetable() { 
        return vegetable; 
    }
}

@VegetableSaladScope
@Component(dependencies = SaladDressingComponent.class, modules = VegetableSaladModule.class)
public interface VegetableSaladComponent { 
    void inject(MainActivity activity);
}

加一段代码

@Inject
FruitSalad fruitSalad;

@Inject
VegetableSalad vegetableSalad;

SaladDressingComponent saladDressingComponent = DaggerSaladDressingComponent.builder() 
    .saladDressingModule(new SaladDressingModule(别人给的saladDressing)) 
    .build();

DaggerFruitSaladComponent.builder()        
    .saladDressingComponent(saladDressingComponent)        
    .fruitSaladModule(new FruitSaladModule(new Fruit()))        
    .build().inject(this);

DaggerVegetableSaladComponent.builder()        
    .saladDressingComponent(saladDressingComponent)        
    .vegetableSaladModule(new VegetableSaladModule(new Vegetable()))        
    .build().inject(this);

哎,反正我也不晓得有没有讲清楚,然后讲一下遇到的坑。

@AScope
@Component(modules = AModule.class)
public interface AComponent { 
}

@Component(dependencies = AComponent.class, modules = BModule.class)
public interface BComponent { 
}

这样写会报错,BComponent(没有加@Scope)不能dependencies(依赖)AComponent(加了@Scope),所以你需要给BComponent也加上@Scope

@XScope
@Component(modules = AModule.class)
public interface AComponent { 
}

@XScope
@Component(dependencies = AComponent.class, modules = BModule.class)
public interface BComponent { 
}

这样也会报错,两个Component有依赖关系是不能用相同的@Scope的,自己再定义一个@Scope然后把其中一个换掉就可以了


有什么理解不到位的地方欢迎指点惹=。=