<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                #Dagger2圖文完全教程 BEGIN ---------- 本文屬代碼GG原創,非經本人同意,禁止轉載。 需要交流,聯系微信:code_gg_boy 更多精彩,時時關注微信公眾號code_gg_home ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/0.jpg) 沒有更多開場白,直接說下我對它的理解。 Dagger2 是一個Android依賴注入框架。而android開發當前非常流行的非MVP模式莫屬了,Dagger2的目標便是將MVP中的V P 進一步解耦,達到模塊化最大的解耦,使得代碼更容易維護。 舉個栗子:有個A對象 B對象 和C對象,如果C對象創建需要A和B,那么我們是不是需要構造里面傳入參數A和參數B,然后在使用的地方如下寫個代碼: ```java C c=new C(new A(),new B()); ``` 如果我們使用了Dagger2時候,我們就不需要管這些了,只需要關聯住能提供創建A 和 B的地方 ,然后在需要C的地方寫下: ```java @Inject C c; ``` 然后在這個類的初始化地方進行注入即可。 我們初步來看,會發現Dagger2優勢不大,沒什么吸引人的,那么請你靜下心來,看完再得出結論。 ---------- 閑話休敘,我們來直接上代碼:(常規寫法) #1 編寫一個類: ```java public class Test3 { public Test3() { } } ``` #2 使用的地方 ```java public class MainActivity extends AppCompatActivity { Test3 test3; @Override protected void onCreate(Bundle savedInstanceState) { //..... test3 = new Test3(); } } ``` ---------- - 如果我們改為使用Dagger2的方式的話,則可以寫成如下方式: #1 創建一個類 使用了注解方式,使得Dagger2能找到它。 ```java public class Test3 { //這里可以看到加入了注解方式 @Inject public Test3() { } } ``` #2 新增一個對象: ```java @Singleton //用這個標注標識是一個連接器 @Component() public interface MainActivityComponent { //這個連接器要注入的對象。這個inject標注的意思是,我后面的參數對象里面有標注為@Inject的屬性,這個標注的屬性是需要這個連接器注入進來的。 void inject(MainActivity activity); } ``` #3 調用的地方改為: ```java public class MainActivity extends AppCompatActivity { //加入注解,標注這個test3是需要注入的 @Inject Test3 test3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //使用組件進行構造,注入 DaggerMainActivityComponent.builder().build().inject(this); } ``` ---------- 這是最簡單的一種使用了。首先我們看到,第一印象是我去,這個更復雜了啊。我只能說確實,因為這個是它對的最基礎的使用,看起來很笨拙,但是當它在大型項目里面,在依賴更多的情況下,則會發生質的飛躍,會發現它非常好用,并且將你需要傳遞的參數都隱藏掉,來實現解耦。 ---------- 我先說下Dagger2的注釋思路:關鍵的點是@Component,這個是個連接器,用來連接提供方和使用方的,所以它是橋梁。它使用在組件里面標記使用的Module(標記用到了哪個Module,主要是看使用方需要哪些對象進行構造,然后將它的提供方@module寫在這里) 然后我們寫入一個void inject(MainActivity activity); 這里后面的參數,就是我們的使用方了。如此一來,我們在使用的地方,使用類似這種方式(DaggerMainActivityComponent.builder().build().inject(this);)的動作,將使用方類里面的標記 為@Inject的類初始化掉,完成自動初始化的動作。 結構如下: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/1459515096_5841.png) 為了更好的來學習它,我們來依次看看各種使用情況。 #1 常規使用方法 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/1/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/1/2.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/1/3.png) 直接感受下,如何? #2 帶一個參數的效果 ---------- ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/2/1.png) ---------- ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/2/2.png) ---------- ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/2/3.png) ---------- 我們來看一個代碼段,當我們創建兩個實例的時候,發現地址是獨立的。 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/3/1.png) 如果我們想要一樣的地址呢?加上一句話,具體如下: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/3/2.png) 效果便是兩個共用實例啦。 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/3/3.png) #3 換種經常使用的方式 將提供的構造,放入@module里面,具體效果如下: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/4/1.png) 去掉標記的@singleton后 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/4/2.png) 效果變成獨立的啦 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/4/3.png) #4 依賴一個組件的時候 有時我們需要依賴一個組件,這個最常見的用法是,我們App實例里面提供了比如獲取sharepreference的實例,和比如現在代碼里面的LocationManager的實例,我們Activity里面需要這些實例,我們該如何來做呢?看效果: 1:一個AndroidModule 模塊標記 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/1.png) 這個模塊屬于AndroidcationComponent 組件里面 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/2.png) 這里有個關鍵點,就是子組件需要這個里面的某個實例的時候,這里需要使用一個接口,將需要的實例做一個返回動作。這里是LocationManager這一行。 我們的子組件的代碼如下: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/3.png) 對應的Cmodule代碼如下: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/4.png) 再來看下Test3的代碼當前情況: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/5.png) 使用的地方: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/6.png) ---------- 細心的你會發現這里多了一個注釋了,@PerActivity,它是個什么鬼呢? ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/7.png) 這里我們看到它是使用了@Scope的一個注釋,這個注釋的意思就是作用域,在作用域內保持單例,可以直接理解為單例即可。 為什么要新增一個呢,主要是因為各個組件需要獨立出來,因此如果是依賴關系,則需要各自在不同的注釋作用域里面。 我們來看下在Cmodule里面,加上@perActivity注釋后的效果: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/8.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/9.png) 如果去掉呢? ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/10.png) 我們突然發現,它和單例的注釋起的作用一樣啊。so。。。是不是發現什么啦。 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/5/11.png) 因此我們得出一個結論,這里@Singleton 就是一個普通的作用域通道,使用了作用域@Scope注釋的代碼,會變成單例模式。為了驗證我們的思路,作如下測試: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/6/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/6/2.png) 我們將之前的@Singleton用新建的這個替換掉,驗證兩次的生成代碼,發現一模一樣,一模一樣,一模一樣,so。。。 就是這個樣子啦。 #5 自定義一個標記 為什么要自定義標記呢?這個標記不是使用@Scope注釋的哦,是使用@Qualifier 標記的,它的目標是,為了區分如果同時返回類型一樣,比如構造男孩,女孩的基本屬性,性別和名字時候,獲取男孩和女孩都是一個對象,我們該如何區分呢,這個就是關鍵啦。說這么多,真心很煩,直接栗子來啦。 這里稍安勿躁,先來看相同效果的另一個注釋,@Name,這個是Dagger2自帶的一個讓區分,效果如下: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/7/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/7/2.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/7/3.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/7/4.png) 這里@Name可以簡單的一個使用方式,就是它不是區分對象,而是限制使用時候必須加入這個注釋,否則報錯,目的就是讓使用者注意是否使用正確了。 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/7/5.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/7/6.png) 我們使用自己的注釋再來一遍: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/8/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/8/2.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/8/3.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/8/4.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/8/5.png) 對比兩種方式,我們發現使用@Name的時候,后面的注釋名字會敲錯,而我們第二種方式呢,則不會耶,so。。。 我們看下自定義的標記,作為限制出錯,讓強制標注的例子。 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/9/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/9/2.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/9/3.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/9/4.png) #6 子組件(公共組件) 這個出現的目的是為了如果有一個組件,是每次創建實例提供給別人,而恰好其他組件(有多個)里面有需要它,如果只有一個,我們就用依賴搞定啦。那么它就可以定義成子組件,誰需要在誰的組件里面加一下,具體看例子: ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/10/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/10/2.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/10/3.png) #7 懶加載方式 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/11/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/11/2.png) #8 多個綁定方式 ####1 第一種方式 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/2.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/3.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/4.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/5.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/6.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/7.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/8.png) ####2 第二種方式 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/9.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/10.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/11.png) 這里需要注意的就是,在組件里面加入多個綁定的時候,module的里面必須要有一個是@IntoSet 這個作為第一個標記,否則會出錯,可以多個@IntoSet標記。 如果是列表類型的,則使用@ElementsIntoSet就ok了。 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/12/12.png) #9 終極boss ,Map方式 ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/13/1.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/13/2.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/13/3.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/13/4.png) ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/13/5.png) 再加一點,生成的代碼位置在\dagger2Demo\app\build\generated\source\apt\debug\com\xm\dagger2demo,可以直接看生成代碼,更好理解 如上,寫完啦。。 [回到開始地方](#begin) 實戰地方,可以參照 https://github.com/gzsll/TLint 來閱讀啦,收工。 需要交流,聯系微信:code_gg_boy 更多精彩,時時關注微信公眾號code_gg_home ![一個顯示圖](https://github.com/luxiaoming/dagger2Demo/raw/master/images/0.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看