引入Dagger2
首先,我們需要將Dagger2的依賴寫入我們的gradle中,具體配置如下
android {
...
}
dependencies {
...
compile "com.google.dagger:dagger:2.8"
annotationProcessor "com.google.dagger:dagger-compiler:2.8"
provided 'javax.annotation:jsr250-api:1.0'
compile 'javax.inject:javax.inject:1'
...
}
配置好之后就可以使用dagger2了。
注解
這里先講講四種基礎(chǔ)的注解,他們分別是:
- @Inject Inject主要有兩個作用,一個是使用在構(gòu)造函數(shù)上,通過標(biāo)記構(gòu)造函數(shù)讓Dagger2來使用(Dagger2通過Inject標(biāo)記可以在需要這個類實例的時候來找到這個構(gòu)造函數(shù)并把相關(guān)實例new出來)從而提供依賴,另一個作用就是標(biāo)記在需要依賴的變量讓Dagger2為其提供依賴。
- @Provides 用Provides來標(biāo)注一個方法,該方法可以在需要提供依賴時被調(diào)用,從而把預(yù)先提供好的對象當(dāng)做依賴給標(biāo)注了@Injection的變量賦值。Provides主要用于標(biāo)注Module里的方法
- @Module 用Module標(biāo)注的類是專門用來提供依賴的。有的人可能有些疑惑,看了上面的@Inject,需要在構(gòu)造函數(shù)上標(biāo)記才能提供依賴,那么如果我們需要提供的類構(gòu)造函數(shù)無法修改怎么辦,比如一些jar包里的類,我們無法修改源碼。這時候就需要使用Module了。Module可以給不能修改源碼的類提供依賴,當(dāng)然,能用Inject標(biāo)注的通過Module也可以提供依賴
- @Component Component一般用來標(biāo)注接口,被標(biāo)注了Component的接口在編譯時會產(chǎn)生相應(yīng)的類的實例來作為提供依賴方和需要依賴方之間的橋梁,把相關(guān)依賴注入到其中。
example
我們想在activity中調(diào)用Car。下面是car類
public class Car {
@Inject
public Car() {
}
public String run() {
return "car run ";
}
}
給car的構(gòu)造方法添加Inject注解,Dagger2通過Inject注解可以在需要Car這個類實例的時候來找到這個構(gòu)造函數(shù)并把相關(guān)實例new出來。
接下來需要一個Component,代碼如下:
@Component
public interface CarComponent {
void inject(CarActivity activity);
}
添加完CarComponent類后,先build一下,會生成一個DaggerCarComponent的類。DaggerCarComponent是CarComponent的一個實現(xiàn)類。DaggerCarComponent的作用是把需要注入的對象注入到CarActivity里面。
然后在Activity里注入這個Car,代碼如下:
@Inject
Car mCar;
private CarComponent mCarComponent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car);
mCarComponent = DaggerCarComponent.builder().build();
mCarComponent.inject(this);
TextView text = findViewById(R.id.text);
text.setText(mCar.run());
}
運行代碼,效果如下:
上面的例子只是用到了Inject和Component,下面介紹一下另外兩個注解的用法。
假設(shè)Car這個類事第三方的類,而且我們沒有辦法修改這個類(就是不能在這個類的構(gòu)造函數(shù)上加上@Inject的注解),這個時候就需要用到Provides和Module。先把Car的構(gòu)造函數(shù)上的@Inject給去掉,
public class Car {
public Car() {
}
public String run() {
return "car run ";
}
}
然后創(chuàng)建一個Module類,來提供Car的實例,代碼如下:
@Module
public class CarModule {
@Provides
public Car provide(){
return new Car();
}
}
@Provides表明這個是一個提供實例的方法,Dagger2在實例化被注入的對象的時候,會先去找有沒有被@Provides標(biāo)記的提供對象實例的方法,如果沒有就會去找有沒有背@Inject標(biāo)記的構(gòu)造方法。
接下來把Module與Component關(guān)聯(lián)起來,修改CarComponent,添加modules = CarModule.class,代碼如下:
@Component(modules = CarModule.class)
public interface CarComponent {
void inject(CarActivity activity);
}
這里再說明一個問題,我們有兩種方式可以提供依賴,一個是注解了@Inject的構(gòu)造方法,一個是在Module里提供的依賴,那么Dagger2是怎么選擇依賴提供的呢,規(guī)則是這樣的:
- 步驟1:查找Module中是否存在創(chuàng)建該類的方法。
- 步驟2:若存在創(chuàng)建類方法,查看該方法是否存在參數(shù)
- 步驟2.1:若存在參數(shù),則按從步驟1開始依次初始化每個參數(shù)
- 步驟2.2:若不存在參數(shù),則直接初始化該類實例,一次依賴注入到此結(jié)束
- 步驟3:若不存在創(chuàng)建類方法,則查找Inject注解的構(gòu)造函數(shù),看構(gòu)造函數(shù)是否存在參數(shù)
- 步驟3.1:若存在參數(shù),則從步驟1開始依次初始化每個參數(shù)
- 步驟3.2:若不存在參數(shù),則直接初始化該類實例,一次依賴注入到此結(jié)束
然后運行一下代碼,效果和上一個一樣。
下面介紹一個注入TextView的例子:
新建一個TextViewModule,代碼如下:
@Module
public class TextViewModule {
private Context mContext;
public TextViewModule(Context context) {
mContext = context;
}
@Provides
public TextView provideTextView() {
return new TextView(mContext);
}
}
provideTextView用于提供TextView。然后把這個Module和Component關(guān)聯(lián)起來。代碼如下:
@Component(modules = {CarModule.class, TextViewModule.class})
public interface CarComponent {
void inject(CarActivity activity);
}
在modules中再加一個TextViewModule.class就好。然后把TextView注入到CarActivity中,最后再把這個TextView添加到Activity的content view里面。代碼如下:
public class CarActivity extends Activity {
@Inject
Car mCar;
@Inject
TextView mTextView;
private CarComponent mCarComponent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car);
mCarComponent = DaggerCarComponent.builder().textViewModule(new TextViewModule(this)).build();
mCarComponent.inject(this);
ViewGroup root = findViewById(android.R.id.content);
root.addView(mTextView);
mTextView.setText("added TextView");
TextView text = findViewById(R.id.text);
text.setText(mCar.run());
}
}
這里要說一下textViewModule(new TextViewModule(this))這句代碼,這里是給Component添加一個TextViewModule的實例,每當(dāng)Component關(guān)聯(lián)一個Module的時候,Component的實現(xiàn)類都會增加一個傳入Module實例的方法,DaggerCarComponent中就會有如下兩個方法。
public Builder carModule(CarModule carModule) {
this.carModule = Preconditions.checkNotNull(carModule);
return this;
}
public Builder textViewModule(TextViewModule textViewModule) {
this.textViewModule = Preconditions.checkNotNull(textViewModule);
return this;
}
那我們?yōu)樯恫挥玫鬰arModule來傳入一個CarModule對象呢?因為CarModule有一個無參數(shù)的構(gòu)造方法,Dagger會默認去掉這個無參數(shù)的構(gòu)造方法,而TextViewModule則沒這種構(gòu)造方法,這需要在用的時候調(diào)運textViewModule(new TextViewModule(this))。
下面來看一下運行效果: