dagger2系列之Scope

  Dagger的Scope註解表明的是做用域,經過實現自定義@Scope註解,標記當前生成對象的使用範圍,標識一個類型的注射器只實例化一次,在同一個做用域內,只會生成一個實例,app

而後在此做用域內共用一個實例。這樣看起來很像單例模式,咱們能夠查看@Singleton其實就是@Scope的一個默認實現而已。固然,你得是同一個Component對象來生成ide

Dagger爲咱們提供了Scope的一種實現Singleton。單例???no!!!no!!!no!!!不是這麼簡單的。先看一下它的代碼:學習

@Scope
@Documented
@Retention(RUNTIME)
public @interface Singleton {}

  在Dagger中咱們能夠自定義註解,譬如咱們能夠照着上面的模版自定義一個PreScope。ui

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface PreScope {
}

  咱們自定義的這個PreScope註解也有和Singleton一樣的做用(單例)。下面看一下它的使用!!!this

@Module
public class AppModule7 {

    private App7 app;

    public AppModule7(App7 app) {
        this.app = app;
    }

    @Provides
    @Singleton
    public App7 provideApp() {
        return app;
    }

    @Provides
    @Singleton
    Context provideApplicationContext() {
        return app.getApplicationContext();
    }

}

@Singleton
@Component(modules = {AppModule7.class})
public interface AppComponent7 {
    void inject(App7 app);

    //由於AppComponent會被dependencies,因此此處Module中提供的內容,咱們須要在此處聲明一下
    App7 getApp();
    Context getApplicationContext();
}

 

@Module
public class ActivityModule7 {

    @Provides
    @PreScope
    public DependencyPresenter7 getDependencyPresenter(App7 app){
        return new DependencyPresenter7(app);
    }
}

@PreScope
@Component(modules = ActivityModule7.class,dependencies = AppComponent7.class)
public interface ActivityComponent7 {
    void inject(TargetActivity71 activity71);
    void inject(TargetActivity72 activity72);
}

public class App7 extends Application {

    private AppComponent7 mAppComponent7;
    private ActivityComponent7 mActivityComponent7;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppComponent7 = DaggerAppComponent7.builder().appModule7(new AppModule7(this)).build();
        mAppComponent7.inject(this);
        mActivityComponent7 = DaggerActivityComponent7.builder().appComponent7(mAppComponent7).build();
    }

    public AppComponent7 getAppComponent7(){
        return mAppComponent7;
    }
    public ActivityComponent7 getActivityComponent7(){
        return mActivityComponent7;
    }

}

public class DependencyPresenter7 {
    public DependencyPresenter7(App7 app){
        Log.d("Dagger.class","DependencyPresenter7-----構造器被調用--------");
    }

    public void printMethod(){
        Log.d("Dagger.class","DependencyPresenter7-----printMethod()-----");
    }

}

public abstract class BaseActivity712 extends Activity {

    private ActivityComponent7 mActivityComponent7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App7 app712 = (App7) getApplication();
        mActivityComponent7 = app712.getActivityComponent7();
        inject(mActivityComponent7);
    }

    public abstract void inject(ActivityComponent7 activityComponent7);
}

public class TargetActivity71 extends BaseActivity712 {

    @Inject
    DependencyPresenter7 mDependencyPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("Dagger.class","TargetActivity71---mDependencyPresenter地址值-------"+mDependencyPresenter.toString());
    }

    @Override
    public void inject(ActivityComponent7 activityComponent7) {
        activityComponent7.inject(this);
    }

    public void jump(View v){
        startActivity(new Intent(this,TargetActivity72.class));
    }
}

public class TargetActivity72 extends BaseActivity712 {

    @Inject
    DependencyPresenter7 mDependencyPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("Dagger.class","TargetActivity72---mDependencyPresenter地址值-------"+mDependencyPresenter.toString());
    }

    @Override
    public void inject(ActivityComponent7 activityComponent7) {
        activityComponent7.inject(this);
    }
}

  上面的代碼咱們使用了Dagger默認實現的@Singleton和自定義的@PreScope註解一塊兒實現單例。咱們之因此不僅用dagger提供的Singleton並且還用自定義的,是由於咱們使用的域不同,當不一樣的Component有依賴依賴或包含關係的時候就不能用一樣的Scope,就須要咱們自定義。spa

下面是我在學習過程當中總結的結論:code

    1  必須使用同一個Component對象來生成,也就是DaggerActivityComponent7.builder().build()只能執行一次。對象

    2  Component和它的Module要使用相同的Scope。同一個Module中須要使用一樣的Scope,不然相應的Component不知道使用什麼Scopeblog

    3  有依賴關係或者包含關係的Component不能使用一樣的Scope。ci

  上面的三條結論就是在學習使用Singleton的時候總結出的,你們在學習的時候,最好本身也驗證一下。

相關文章
相關標籤/搜索