《Android進階之光》--Dagger2

來源:互聯網
上載者:User

標籤:get   pos   protect   ica   provides   dac   his   apply   sel   

No1:

Project的build.gradle檔案添加

buildscript{    dependencies{        ...classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘    }}

Module:app的build.gradle添加

apply plugin: ‘com.neenbedankt.android-apt‘...dependencies{    ...    apt ‘com.google.dagger:dagger-compiler:2.7‘    compile ‘com.google.dagger:dagger:2.7‘}

No2:

@Inject、@Component

public class Watch{    @Inject    public Watch(){    }    public void work(){    }}
@Componentpublic interface MainActivityComponent{    void inject(MainActivity activity);}

調用

public class MainActivity extends AppCompatActivity{    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    DaggerMainActivityComponent.create().inject(this);//2    watch.work();}

No3:

@Module、@Provides

@InjectGson gson;
@Modulepublic class GsonModule{    @Provides    public Gson provideGson(){        return new Gson();    }}
@Component(modules = GsonModule.class)public interface MainActivityComponent{    void inject(MainActivity activity);}

調用

public class MainActivity extends AppCompatActivity{    @Inject    Gson gson;    @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        DaggerMainActivityComponent.create().inject(this);        String jsonData = "{‘name‘:‘zhangwuji‘,‘age‘:20}";        Man man = gson.fromJson(jsonData,Man.class);    }}

No4:

@Named、@Qualifier

@Modulepublic class EngineModule{    @Provides    @Named("Gasoline")    public Engine provideGasoline(){        return new GasolineEngine();    }    @Provides    @Named("Diesel")    public Engine provideDiesel(){        return new DieselEngine();    }}

調用

public class Car{    private Engine engine;    @Inject    public Car(@Named("Diesel") Engine engine){        this.engine = engine;    }    public String run(){        return engine.work();    }}

-------------------------------------

@Qualifier@Retention(RUNTIME)public @interface GasoLine{}@Qualifier@Retention(RUNTIME)public @interface Diesel{}
@Modulepublic class EngineModule{    @Provides    @Gasonline    public Engine provideGasoline(){        return new GasolineEngine();    }    @Provides    @Diesel    public Engine provideDiesel(){        return new DieselEngine();    }}
public class Car{    private Engine engine;    @Inject    public Car(@Gasoline Engine engine){        this.engine = engine;    }    public String run(){        return engine.work();    }}

No5:

 @Singleton、@Scope

@InjectGson gson;@InjectGson gson1;
@Modulepublic class GsonModule{    @Singleton    @Provides    public Gson provideGson(){        return new Gson();    }}
@Singleton@Component(modules = GsonModule.class)public interface MainActivityComponent{    void inject(MainActiviy activity);}

調用

public class MainActivity extends AppCompatActivity{    private static final String TAG="Dagger2";    @Inject    Gson gson;    @Inject    Gson gson1;    @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        DaggerMainActivityComponent.create().inject(this);    }}

-----------------------------

@Scope@Documented@Retention(RUNTIME)public @interface Singleton{}
@Scope@Rentention(RUNTIME)public @interface ApplicationScope{}
@Modulepublic class GsonModule{    @ApplicationScope    @Provides    public Gson provideGson(){        return new Gson();    }}
@ApplicationScope@Component(modules=GsonModule.class)public interface ActivityComponent{    void inject(MainActivity activity);    void inject(SecondActivity activity);}
public class App extends Application{    ActivityComponent activityComponent;    @Override    public void onCreate(){        super.onCreate();        activityComponent = DaggerActivityComponent.builder().build();    }    public static App get(Context context){        return (App)context.getApplicationContext();    }    ActivityComponent getActivityComponent(){        return activityComponent;    }}

調用

public class MainActivity extends AppCompatActivity{    private static final String TAG="Dagger2";    private Button bt_jump;    @Inject    Gson gson;    @Inject    Gson gson1;    @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        App.get(MainActivity.this).getActivityComponent().inject(this);        onClick();    }    private void onClick(){        bt_jump = (Button)findViewById(R.id.bt_jump);        bt_jump.setOnClickListener(new View.OnClickListener(){            @Override            public void onClick(View view){                Intent intent = new Intent(MainActivity.this,SecondActivity.class);                startActivity(intent);            }        });    }}

No6:

@Component

public class Swordsman{    @Inject    public Swordsman{    }    public String fighting(){        return "欲為大樹,莫與草爭";    }}
@Modulepublic class SwordsmanModule{    @Provides    public Swordsman provideSwordsman(){        return new Swordsman();    }}@Component(modules = SwordsmanModule.class)public interface SwordsmanComponent{    Swordsman getSwordsman();}
@ApplicationScope@Component(modules = GsonModule.class,dependencies = SwordsmanComponent.class)public interface ActivityComponent{    void inject(MainActivity activity);    void inject(SecondActivity activity);}
public class App extends Application{    ActivityComponent activityComponent;    @Override    public void onCreate(){        super.onCreate();        activityComponent = DaggerActivityComponent.builder();        swordsmanComponent(DaggerSwordsmanComponent.builder().build()).build();    }    public static App get(Context context){        return (App)context.getApplicationContext();    }    ActivityComponent getActivityComponent(){        return activityComponent;    }}

調用

public class SecondActivity extends AppCompatActivity{    @Inject    Swordsman swordsman;    @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        App.get(SecondActivity.this).getActivityComponent().inject(this);        String sd = swordsman.fighting(;    }}

No7:

懶載入:在@Inject的時候不初始化,而是使用的時候,調用get方法來擷取執行個體

public class SecondActivity extends AppCompatActivity{    private static final String TAG="Dagger2";    @Inject    Lazy<Swordsman> swordsmanLazy;    @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        App.get(SecondActivity.this).getActivityComponent().inject(this);        Swordsman swordsman = swordsmanLazy.get();        swordsman.fighting();        String sd1 = swordsman.fighting();    }}

 

《Android進階之光》--Dagger2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.