google四件套之Dagger2
前言
網(wǎng)上都說Dagger2是比較難上手的,我在看了大量資料和使用時(shí)也遇到了很多不懂或者模糊的知識(shí)點(diǎn),而且大部分博客資料都比較古老。突然有那么一瞬間,突然明白了所以然,故總結(jié)了4篇文章。話說在java中使用還是很繁瑣的,不要怕帶你真正上手,并運(yùn)用到我們的Android項(xiàng)目中去。
本次Dagger2講解總共分4篇:
1、Dagger2基礎(chǔ)知識(shí)及在Java中使用(1)
2、Dagger2基礎(chǔ)知識(shí)及在Java中使用(2)
3、Dagger2進(jìn)階知識(shí)及在Android中使用
4、Dagger2華麗使用在MVP框架中
首先簡(jiǎn)單申明下,Dagger2的好處不是本文的重點(diǎn)。大家可以自行百度。Dagger2是依賴注解框架,像我們之間的butterknife也是這樣的框架,想這樣的框架依賴一般都有2行。第二行是以annotationProcessor開頭的。這其實(shí)是apt的工具,而且這樣的依賴注解框架不會(huì)影響性能(不是反射機(jī)制),在編譯的時(shí)候,apt把要用的代碼生成。所以大可放心使用
再舉我理解的例子(大家不要全信,哈哈稍微不恰當(dāng)):@Component相當(dāng)于一個(gè)注射器(記住是接口);@Module相當(dāng)于注射液,就是數(shù)據(jù)源(記住這里是類或者抽象類),此時(shí)要把注射液放入指定哪個(gè)注射器如:@Component( modules = … );@Inject 相當(dāng)于標(biāo)注被注射體。
之后的講解都是走完簡(jiǎn)單的流程,實(shí)現(xiàn)功能,然后講大概理解。貼在博客上的代碼,可能會(huì)省略部分代碼,便于理解。github上的Demo及注釋,非常詳細(xì),接近完美0-0#
如果是完全沒了解過,關(guān)于Dagger一些標(biāo)注的具體介紹和理解,推薦這里有3篇介紹標(biāo)注的意思和怎么工作的
首先添加依賴
implementation 'com.google.dagger:dagger:2.24'
annotationProcessor "com.google.dagger:dagger-compiler:2.24"
1
2
1、@Inject & @Component 的簡(jiǎn)單使用(不帶@Module)
首先隨便定義個(gè)類:Person,無參構(gòu)造方法用@Inject標(biāo)注:
public class Person {
? ? @Inject
? ? public Person() {
? ? }
}
1
2
3
4
5
6
然后定義我們的 Component(這里稍微提一下,如果一個(gè)頁(yè)面定義多個(gè)Component,你build的時(shí)候報(bào)錯(cuò),是不是)
這里的命名規(guī)則最好是以我們頁(yè)面類名+Component,這樣比較清晰。用@Component標(biāo)注,里面有個(gè)void方法,方法名隨意定,建議用inject最好,當(dāng)然也是清晰,參數(shù)是我們需要依賴注解的頁(yè)面:
@Component
public interface AstudyActivityComponent {
? ? void injectTo(AstudyActivity astudyActivity);
}
1
2
3
4
做好上面步驟后,點(diǎn)開studio里build標(biāo)簽下的Make Project。讓apt幫我們生成代碼,一般生成代碼為Dagger+你定義Component的類名。
之后這個(gè)步驟不再重復(fù),就是你寫完準(zhǔn)備代碼的時(shí)候一定要讓apt生成代碼,Make Project下
然后在我們的Activity里:
public class AstudyActivity extends BaseActivity {
? ? @Inject
? ? Person person;
? ??
? ? @Override
? ? //這里是我封裝的onCreate,省略部分代碼,只為理解,之后都請(qǐng)忽略!
? ? protected void processLogic() {
? ? ? ? //第一種
? ? ? ? DaggerAstudyActivityComponent.create().injectTo(this);
? ? ? ? //第二種
? ? ? ? //DaggerAstudyActivityComponent.builder().build().injectTo(this);
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
在我們的Activity里build下我們的Component,然后注冊(cè)在我們的Activity里,就可以使用通過我們的@Inject使用我們的person了。
這里初始化有2種:
1、DaggerAstudyActivityComponent.create().injectTo(this);
2、DaggerAstudyActivityComponent.builder().build().injectTo(this); 這個(gè)使用module傳值一定要使用
那么一個(gè)簡(jiǎn)單的使用就實(shí)現(xiàn)了,這里忽略了new的過程,從這個(gè)過程就知道他解耦的實(shí)現(xiàn)了。
簡(jiǎn)單使用大致步驟( 看懂請(qǐng)略過 ):
第一步:用Inject標(biāo)注,告訴dagger2可以實(shí)例化這個(gè)類,如:Person
第二步:使用注解Component,表示要將依賴注入到AstudyActivity里
第三步:使用android studio build下的Make Project生成代碼,使他自動(dòng)生成DaggerComponent生成的類,類名是:Dagge+我們定義的Component的名字
2、帶@Module的使用
為什么會(huì)有module的概念,比如上面的Person的構(gòu)造方法可以用@Inject標(biāo)注,但是引入的第三方庫(kù)可是沒有辦法加的,所以這里使用@Module可以解決這個(gè)問題。
這里我們定義個(gè)Human,假裝他是第三方類庫(kù),里面沒有使用@Inject
public class Human {
? ? public Human() {
? ? }
}
1
2
3
4
5
接下的步驟先定義我們的數(shù)據(jù)源Module,也就是先定義初始化的地方,之前Person的構(gòu)造方法是用@Inject。首先命名規(guī)則最好加上Module,用@Module標(biāo)注。然后里面定義個(gè)方法,用 @Provides標(biāo)注。返回值為我們需要初始化的類,方法名最好是以Provides結(jié)尾。其實(shí)這里可以定義多個(gè)方法,后面說
@Module
public class BstudyActivityModule {
? ? @Provides
? ? Human humanProvides(){
? ? ? ?return new Human();
? ? }
}
1
2
3
4
5
6
7
然后是我們的Component。這里與之前不同的是(modules = BstudyActivityModule.class),這就相當(dāng)于把注射液放進(jìn)注射器。這里可以有多個(gè)Module,后面說
@Component(modules = BstudyActivityModule.class)
public interface BstudyActivityComponent {
? ? void injectTo(BstudyActivity bstudyActivity);
}
1
2
3
4
Make Project后,在Activity里操作與之前一模一樣。
帶Module使用大致步驟( 看懂請(qǐng)略過 )
1、假設(shè)Human不可隨意更改,沒有@Inject標(biāo)注(第三方類庫(kù),不是你項(xiàng)目里的代碼肯定沒有@Inject)用@module標(biāo)注BstudyActivityModule,用@Provides標(biāo)注方法的返回值就是我們需要inject的類型
2、編寫Component接口使用@Component標(biāo)注這個(gè)接口,并使用modules=的方法鏈接上第一步中編寫的Module類;
3、接下來就和AstudyActivity中的使用方式一樣了
3、通過Module傳參
這個(gè)其實(shí)不重要,重要的引出4,5的概念。明白這步,后面才好理解。
首先我們假設(shè)2個(gè)類,女人類,和靈魂類:且靈魂類有個(gè)錢的屬性。靈魂類又是女人的屬性。靈魂類如下:
public class Soul {
? ? private int money;
? ? public Soul() {
? ? }
? ? public int getMoney() {
? ? ? ? return money;
? ? }
? ? public void setMoney(int money) {
? ? ? ? this.money = money;
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
女人如下:
public class Woman {
? ? private Soul soul;
? ? public Soul getSoul() {
? ? ? ? return soul;
? ? }
? ? public Woman(Soul soul) {
? ? ? ? this.soul = soul;
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
首先還是定義我們的Module先。既然可以傳參,當(dāng)然是有個(gè)money的屬性。最終我們依賴注解是要使用Woman類。我們的providesWoman方法用@Provides標(biāo)注,這個(gè)時(shí)候他回去找Soul的初始化,先通過@Provides去找Soul。這個(gè)時(shí)候找到了providesSoul。這樣就形成了女人類。假如這個(gè)時(shí)候沒有providesSoul。他會(huì)去找Soul類里有沒有用@Inject標(biāo)注的構(gòu)造函數(shù)。如果還沒有,那么不好意思。出錯(cuò)
@Module
public class CstudyModule {
? ? private int money;
? ??
? ? public CstudyModule(int money) {
? ? ? ? this.money = money;
? ? }
? ? @Provides
? ? Soul providesSoul() {
? ? ? ? Soul soul = new Soul();
? ? ? ? soul.setMoney(this.money);
? ? ? ? return soul;
? ? }
? ? @Provides
? ? Woman providesWoman(Soul soul) {
? ? ? ? return new Woman(soul);
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
接下來是Component,沒有變化
@Component(modules = CstudyModule.class)
public interface CstudyActivityComponent {
? ? void injectTo(CstudyActivity cstudyActivity);
}
1
2
3
4
Activity有些許變化,當(dāng)然是傳參了。我們給女人的靈魂傳了100塊,對(duì),女人只值100塊!
public class CstudyActivity extends BaseActivity {
? ? @Inject
? ? Woman woman;
? ? @Override
? ? protected void processLogic() {
? ? ? ? DaggerCstudyActivityComponent.builder()
? ? ? ? ? ? .cstudyModule(new CstudyModule(100))
? ? ? ? ? ? .build().injectTo(this);
? ? }
}
1
2
3
4
5
6
7
8
9
10
注意點(diǎn)( 看懂請(qǐng)略過 ):
在Module的構(gòu)造函數(shù)帶有參數(shù)且參數(shù)被使用的情況下,所生產(chǎn)的Component類就沒有create()方法了。
在這里的module如果沒有providesSoul()方法的話,還有一種情況只要在Soul的構(gòu)造方法有@Inject也是可行的。
4、使用@Component.Builder(需先了解 3、通過Module傳參)
我們把3、通過Module傳參apt生成的代碼點(diǎn)開DaggerCstudyActivityComponent;看下圖是不是發(fā)現(xiàn)了一個(gè)Builder類,這是apt幫我們自動(dòng)生成的,我們當(dāng)然也能自己實(shí)現(xiàn)
還是以3、通過Module傳參的例子,我們不用系統(tǒng)幫我們生成的Builder,自己定義。前面的步驟都一樣,直接來看我們的Component。
自己定義個(gè)接口類Builder,并用@Component.Builder標(biāo)注里面有2個(gè)方法:
方法一:是返回值Builder的方法,這里如果傳module就會(huì)以我們傳的為主,否則他會(huì)幫我們生成一個(gè)money為0的module。當(dāng)然你也隨意傳數(shù)據(jù)類型,只不過無效。可以試試,
方法二:是返回值為當(dāng)前Component的方法,方法名其實(shí)都可以自定義,當(dāng)最好以規(guī)范為主,用習(xí)慣了就明白了
@Component(modules = CstudyModule.class)
public interface DstudyActivityComponent {
? ? void injectTo(DstudyActivity dstudyActivity);
? ??
? ? @Component.Builder
? ? interface Builder {
? ? ? ? Builder cstudyModule(CstudyModule cstudyModule);
? ? ? ? DstudyActivityComponent build();
? ? }
}
1
2
3
4
5
6
7
8
9
10
Activity里使用是一樣的。只不過我們把系統(tǒng)自動(dòng)幫我們生成的,自己去寫了而已。還是貼下Activity代碼吧
public class DstudyActivity extends BaseActivity {
? ? @Inject
? ? Woman dWoman;
? ? @Override
? ? protected void processLogic() {
? ? ? ? DaggerDstudyActivityComponent.builder()
? ? ? ? ? ? .cstudyModule(new CstudyModule(100))
? ? ? ? ? ? .build().injectTo(this);
? ? }
}
1
2
3
4
5
6
7
8
9
10
大致理解和總結(jié)為( 看懂請(qǐng)略過 ):
通過我們cstudy的內(nèi)容,你可以點(diǎn)開cstudyModule查看源碼,可以看到有個(gè)Builder cstudyModule(CstudyModule cstudyModule){}。這是dagger2自動(dòng)生成的(你還可以通過,app/build/generated/source/apt/debug/你的包名/DaggerAppComponent.java 目錄下找到)
所以@Component.Builder的用法,用module傳參的例子。其他都不用變,要變的是Component,定義個(gè)Builder并用@Component.Builder標(biāo)注。這里有2個(gè)方法:
方法一:是返回值Builder的方法,這里如果傳module就會(huì)以我們傳的為主,否則他會(huì)幫我們生成一個(gè)money為0的module。當(dāng)然你也隨意傳數(shù)據(jù)類型,只不過無效。可以試試
方法二:是返回值為當(dāng)前Component的方法,方法名其實(shí)都可以自定義,當(dāng)最好以規(guī)范為主,用習(xí)慣了就明白了
5、使用@BindsInstance(需先了解 4、使用@Component.Builder)
這個(gè)時(shí)候你又說了,傳參,我們總是要new CstudyModule(100)。本來說Dagger2在使用的時(shí)候省略new的過程,解耦。但這里還要new,很low是不是。不急不急,強(qiáng)大的google把一切都想好了。這個(gè)時(shí)候遇到一個(gè)新的標(biāo)注@BindsInstance。
@BindsInstance 大致這里可以理解為幫我們省去寫類的構(gòu)造方法,而直接去賦值
省掉構(gòu)造方法,那么當(dāng)然是首先改我們的Module。我們?nèi)サ鬗odule的構(gòu)造方法及money成員變量屬性,把money加到providesSoul里成型參。看到這里,這里又可理解為@BindsInstance 其實(shí)去找@Provides標(biāo)記的方法的參數(shù),假如類型一致就去初始化
@Module
public class EstudyModule {
? ? @Provides
? ? Soul providesSoul(int money) {
? ? ? ? Soul soul = new Soul();
? ? ? ? soul.setMoney(money);
? ? ? ? return soul;
? ? }
? ? @Provides
? ? Woman providesWoman(Soul soul) {
? ? ? ? return new Woman(soul);
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
然后是修改的Component,改完Module,當(dāng)然是modules = EstudyModule.class。這些我就忽略了,看了上面的步驟你也明白,我就直接說關(guān)鍵地方了。用@BindsInstance標(biāo)注我們返回值為Builder的方法。里面的參數(shù)改成我們的int Money。當(dāng)然改成我們用@Provides標(biāo)注的類型其實(shí)都可以,只不過這里你如果改成Soul soul,當(dāng)然你初始化還是要傳new Soul。過程就是這個(gè)過程
@Component(modules = EstudyModule.class)
public interface EstudyActivityComponent {
? ? void injectTo(EstudyActivity estudyActivity);
? ??
? ? @Component.Builder
? ? interface Builder {
? ? ? ? @BindsInstance
? ? ? ? Builder initMoney(int money);
? ? ? ? EstudyActivityComponent build();
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
最后是我們的Activity
public class EstudyActivity extends BaseActivity {
? ? @Inject
? ? Woman woman;
? ? @Override
? ? protected void processLogic() {
? ? ? ? DaggerEstudyActivityComponent.builder()
? ? ? ? ? ? .initMoney(100)
? ? ? ? ? ? .build().injectTo(this);
? ? }
}
1
2
3
4
5
6
7
8
9
10
看到這里,是不是覺得Dagger2還比較有意思。更有意思的在后面。當(dāng)然也越來越繞了,但是你得興奮起來,精髓啊。
6、Component依賴Component,使用dependence
這里我們以Activity和Fragment為例。假設(shè)我們?cè)貯ctivity依賴注入Human類,此時(shí)在Fragment里使用
先看定義Module,和之前一樣,沒什么區(qū)別
@Module
public class FstudyActivityModule {
? ? @Provides
? ? Human providesHuman() {
? ? ? ? return new Human();
? ? }
}
1
2
3
4
5
6
7
再建ActivityComponent當(dāng)然這里,你也可以加上void inject(FstudyActivity fstudyActivity)。重要一點(diǎn)是我們要把依賴注入的類返回出去,定義方法provideHuman,因?yàn)槭荂omponent依賴Component。所以也能理解
@Component(modules = FstudyActivityModule.class)
public interface FstudyActivityComponent {
? ? Human provideHuman();
}
1
2
3
4
再使用dependencies建FragmentComponent暫且可以理解為子Component,因?yàn)楹竺嬲娴挠凶覥omponent。dependencies = FstudyActivityComponent.class寫上我們的父Component。后面是注入到Fragment里
@Component(dependencies = FstudyActivityComponent.class)
public interface TestFragmentComponent {
? ? void inject(TestFragment testFragment);
}
1
2
3
4
在Activity里,要先生成ActivityComponent,然后提供個(gè)方法,把父Component提供給Fragment
public class FstudyActivity extends BaseActivity {
? ? private FstudyActivityComponent fstudyActivityComponent;
? ? @Override
? ? protected void processLogic() {
? ? ? ? fstudyActivityComponent = DaggerFstudyActivityComponent.create();
? ? }
? ? public FstudyActivityComponent getFstudyActivityComponent() {
? ? ? ? return fstudyActivityComponent;
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
在Fragment里
public class TestFragment extends BaseFragment {
? ? @Inject
? ? Human human;
? ? @Override
? ? protected void processLogic(Bundle savedInstanceState) {
? ? ? ? FstudyActivityComponent fstudyActivityComponent = ((FstudyActivity) getActivity()).getFstudyActivityComponent();
? ? ? ? DaggerTestFragmentComponent.builder()
? ? ? ? ? ? ? ? .fstudyActivityComponent(fstudyActivityComponent)
? ? ? ? ? ? ? ? .build().inject(this);
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
好了,在fragment可以使用human了。在java里使用,確實(shí)很繞,代碼多的讓你難以接受。建議先理解,后面出的一篇在Android中使用,你會(huì)很爽。
大致理解和總結(jié)為( 看懂請(qǐng)略過 ):
1、假設(shè)我們用Human注入,這里的FstudyActivityModule 和之前的Module一樣,正常
2、FstudyActivityComponent把要注入的類返回
3、在TestFragment方面,我們新建一個(gè)TestFragmentComponent 依賴 FstudyActivityComponent;
4、在FstudyActivity自定義一個(gè)方法把FstudyActivityComponent提供出去,供TestFragment使用
5、在TestFragment,注冊(cè)下就OK了。很繞,個(gè)人建議先明白這個(gè)流程就好了
7、Component依賴Component,使用@subComponent(這個(gè)和 【標(biāo)題6】 實(shí)現(xiàn)的是同一個(gè)效果)
雖然是實(shí)現(xiàn)同一個(gè)效果,但是方式不同,目的是讓你更多了解Dagger2。同樣以上面的例子。Module和上面一樣不變(我這里是為了Demo區(qū)域化,雖然類名不同,但是內(nèi)容是一致的)
首先建子Component,FragmenComponent,用@Subcomponent標(biāo)注,并注入我們的Fragment里。為什么先建子Component呢。因?yàn)樽覥omponent要在父Component返回,繞不繞!!
@Subcomponent
public interface DemoFragmentComponent {
? ? void inject(DemoFragment demoFragment);
}
1
2
3
4
然后是父Component,ActivityComponent,父Component一切正常,返回值是子Component
@Component(modules = GstudyActivityModule.class)
public interface GstudyActivityComponent {
? ? DemoFragmentComponent demoFragmentComponent();
}
1
2
3
4
在Activity里的操作一樣,初始化我們的父Component,并提供方法,返回父Component,供Fragment使用。
然后是Fragment里
public class DemoFragment extends BaseFragment {
? ? @Inject
? ? Human human;
? ? @Override
? ? protected void processLogic(Bundle savedInstanceState) {
? ? ? ? GstudyActivityComponent gstudyActivityComponent = ((GstudyActivity) getActivity()).getGstudyActivityComponent();
? ? ? ? gstudyActivityComponent
? ? ? ? ? ? .demoFragmentComponent()
? ? ? ? ? ? .inject(this);
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
這樣就成功了,可以在Fragment使用human了。看明白了標(biāo)題6,其實(shí)標(biāo)題7原理是一樣的。
大致理解和總結(jié)為( 看懂請(qǐng)略過 ):
1、先建一個(gè)子類Component,用@subComponent標(biāo)注,DemoFragmentComponent
2、然后建父類Component: GstudyActivityComponent,定義個(gè)方法,返回子類Component。
3、在GstudyActivity自定義一個(gè)方法把GstudyActivityComponent提供出去,供DemoFragment使用
4、在DemoFragment,注冊(cè)下,就好了。大致和dependencies類似
注意:但注冊(cè)的時(shí)候?qū)懛ú煌?#xff0c;之前是通過子Component傳入父Component;而這里是從父Component中獲取子Component,然后直接inject
8、Component依賴Component,使用 @Subcomponent.Builder(和【標(biāo)題6】&【標(biāo)題7】實(shí)現(xiàn)的是一樣的效果)
效果一樣,方式不同,目的還是更了解Dagger2。可以看到這里的標(biāo)注是@Subcomponent.Builder。所以和使用@Subcomponent類似。
好了,還是以上面的例子為例。這里需要改的是父Component和子Componet。這個(gè)時(shí)候我們不免想到@Component.Builder的用法。是不是一樣呢。這個(gè)時(shí)候我只能說類似,但是又不一樣。畢竟這里多了個(gè)sub。我們先看下@Component.Builder的用法,拷貝之前代碼(不知道再去回顧下【標(biāo)題4】)
@Component(modules = CstudyModule.class。)
public interface DstudyActivityComponent {
? ? void injectTo(DstudyActivity dstudyActivity);
? ? @Component.Builder
? ? interface Builder {
? ? ? ? Builder cstudyModule(CstudyModule cstudyModule);
? ? ? ? DstudyActivityComponent build();
? ? }
}
1
2
3
4
5
6
7
8
9
10
我們按照找個(gè)方式去寫@Subcomponent.Builder。,@Subcomponent.Builder要使用肯定是在@Subcomponent下,毋庸置疑。首先發(fā)現(xiàn)沒有modules = CstudyModule.class。被@Subcomponent取代了。沒有Module我們就使用無參
@Subcomponent
public interface OtherFragmentComponent {
? ? void inject(OtherFragment otherFragment);
? ? @Subcomponent.Builder
? ? interface Builder {
? ? ? ? Builder noModule(); ? ?
? ? ? ? OtherFragmentComponent build();
? ? }
}
1
2
3
4
5
6
7
8
9
我先告訴告訴你運(yùn)行結(jié)果把,運(yùn)行結(jié)果報(bào)錯(cuò)了。
@Subcomponent.Builder types must have exactly one zero-arg method,
and that method must return the @Subcomponent type. Already found: hstudyActivityModule()
報(bào)錯(cuò)信息如下:意思是不需要Build返回值方法,通過Already found: hstudyActivityModule()知道,已經(jīng)發(fā)現(xiàn)了我們的Module。我們?cè)傧胂脒@個(gè)標(biāo)注的名稱sub,不就是子Component繼承父Componet嗎。而且Dagger2內(nèi)部已經(jīng)默認(rèn)了,所以這里沒有Builder返回值方法。所以正確的子Component
@Subcomponent
public interface OtherFragmentComponent {
? ? void inject(OtherFragment otherFragment);
? ? @Subcomponent.Builder
? ? interface Builder {
? ? ? ? OtherFragmentComponent build();
? ? }
}
1
2
3
4
5
6
7
8
接下來是父Component,返回值當(dāng)然是我們的Builder。
@Component(modules = HstudyActivityModule.class)
public interface HstudyActivityComponent {
? ? OtherFragmentComponent.Builder sonbuilder();
}
1
2
3
4
有人就疑惑了不可以返回子Component嗎。我們假如此時(shí)返回子Component,我先告訴你運(yùn)行報(bào)錯(cuò),信息如下:
Components may not have factory methods for subcomponents that define a builder.
大概意思是:用了@Subcomponent.Builder的話,Component沒有工廠模式方法去創(chuàng)建我們的子Component。好了,就這樣,請(qǐng)?jiān)徫业挠⒄Z(yǔ)四級(jí)!!
Activity還是和之前一樣,初始化我們的父Component,并通過方法返回。Fragment里使用依賴如下
public class OtherFragment extends BaseFragment {
? ? @Inject
? ? Human human;
? ? @Override
? ? protected void processLogic(Bundle savedInstanceState) {
? ? ? ? HstudyActivityComponent hstudyActivityComponent = ((HstudyActivity) getActivity()).getHstudyActivityComponent();
? ? ? ? hstudyActivityComponent.
? ? ? ? ? ? ? ? sonbuilder().build().inject(this);
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
好了,繞來繞去,功能實(shí)現(xiàn)了!看到這里對(duì)Dagger2大致了解了吧。
由于在java里使用比較多。臨時(shí)決定java分2篇。不然博客太長(zhǎng),也沒人看。
本文github Demo地址
————————————————
版權(quán)聲明:本文為CSDN博主「巖漿李的游魚」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/leol_2/article/details/100546108
總結(jié)
以上是生活随笔為你收集整理的google四件套之Dagger2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结合电商支付业务一文搞懂DDD
- 下一篇: 数据湖之iceberg系列(三)iceb