對Dagger2從入門到放棄再到恍然大悟。
第一次接觸這個框架的時候,在網(wǎng)上看了十幾篇文章。看完之后的感受是,二臉懵逼,完全不曉得寫的是什么。
然后過了一段時間,又嘗試著去了解,并且一邊照著別人家的demo依葫蘆畫瓢,但是后來發(fā)現(xiàn),寫不下去,我不曉得我這些代碼寫的是什么意思,然后有扔一邊了。
第三次,又心血來潮,靜下心來硬著頭皮反復看,突然有一絲說是靈感?思路?猜測?立馬抓住,然后整條線立馬完整的串起來了。
添加依賴
//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'
我發(fā)現(xiàn)我之前看這個框架一直沒看懂,有一個原因是太局限于代碼。就是說,框架的使用是為了讓我們打代碼更簡單更容易,而不是為了逼格高(當然逼格確實會變高=。=但是這不是主要目的)從最原始的角度出發(fā),框架都是用來達成某種目的的。Dagger2的目的是依賴注入,說白一點,就是實例化對象,說清楚一點,就是你叫Dagger2幫你實例化對象,這就是最直接的目的,所有的Class和Annotation都是為了達到這個目的輔助工具,所以不必去糾結(jié)這些。下面先舉個例子。。。
比如現(xiàn)在有Fruit,SaladDressing,F(xiàn)ruitSalad三個類,水果,沙拉醬,水果沙拉
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 {
}
好了現(xiàn)在我們自己做一份FruitSalad
FruitSalad fruitSalad = new FruitSalad(new SaladDressing(), new Fruit());
但是我不想自己做,我需要Dagger2幫我做一份。當然FruitSalad(水果沙拉)需要SaladDressing(沙拉醬)和Fruit(水果)才能做,所以SaladDressing(沙拉醬)和Fruit(水果)我依舊自己提供。
不過Dagger2也有自己的規(guī)則,如果想要幫忙做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表明提供這些依賴
現(xiàn)在我把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(水果)都是我自己提供的。
然后現(xiàn)在有人和我說,你的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();
}
所以現(xiàn)在我把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就可以拿到,F(xiàn)ruit(水果)依舊會提供給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);
現(xiàn)在我又想要VegetableSalad(蔬菜沙拉)了,繼續(xù)讓Dagger2幫我做,反正SaladDressing(沙拉醬)已經(jīng)有了,我只要提供蔬菜就好了
@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然后把其中一個換掉就可以了
有什么理解不到位的地方歡迎指點惹=。=