它是如何工作的@BindsInstance dagger 2

Jua*_* T. 8 dagger dagger-2

我最近更新了匕首2.8到2.9匕首.并添加了上一版本的文档,如下所示:

- @BindsInstance为组件构建器添加了轻松绑定在图外部构造的实例.

- 生产者:添加ProducerMonitor.ready (),当所有生产者的输入都可用时调用.

- 删除@Provides(type =...)用法.dagger.multibindings而是使用注释.@Produces.type也被删除了.

- 现在验证所有绑定方法,即使它们在特定用途中未使用 @Component

- @Component.dependencies不能再包括了@Modules.

我想知道这些新功能如何:

谢谢!!

注意:我是dagger 2的新手,但您希望能够最大限度地利用此库.

Thr*_*ian 22

@bindsInstance用于从模块中删除构造函数,并链接获取组件的模块.

没有 @BindsInstance

@Module
public class AppModule {

    private final Application application;

    public AppModule(Application application) {
        this.application = application;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return  application;
    }

    @Provides
    @Singleton
    public SharedPreferences providePreferences() {
        return application.getSharedPreferences("store",
                Context.MODE_PRIVATE);
    }
}
Run Code Online (Sandbox Code Playgroud)

这些模块(ToastMakerModule和SensorControllerModule)用于学习目的,它们获取上下文和实例化,可能对实际示例不实用

public class ToastMaker {

    private Application application;

    public ToastMaker(Application application) {
        this.application = application;
    }

    public void showToast(String message) {
        Toast.makeText(application, message, Toast.LENGTH_SHORT).show();
    }
}

    @Module
    public class ToastMakerModule {

        @Singleton
        @Provides
        ToastMaker provideToastMaker(Application application) {
            return  new ToastMaker(application);

        }
   }

@Singleton
@Component(modules = {AppModule.class, ToastMakerModule.class, SensorControllerModule.class})
public interface AppComponent {
    void inject(MainActivity mainActivity);

    // DaggerAppComponent.build() returns this Builder interface

    @Component.Builder
    interface Builder {
        AppComponent build();

        Builder appModule(AppModule appModule);

        Builder sensorControllerModule(SensorControllerModule sensorControllerModule);

        Builder toastMakerModule(ToastMakerModule toastMakerModule);
    }

}
Run Code Online (Sandbox Code Playgroud)

像这样构建组件

 appComponent = DaggerAppComponent
                .builder()
                .appModule(new AppModule(this))
                .sensorControllerModule(new SensorControllerModule())
                .toastMakerModule(new ToastMakerModule())
                .build();
Run Code Online (Sandbox Code Playgroud)

@BindsInstance

@Module
public class AppModule {

    @Provides
    @Singleton
    public SharedPreferences providePreferences(Application application) {
        return application.getSharedPreferences("data",
                Context.MODE_PRIVATE);
    }
}
Run Code Online (Sandbox Code Playgroud)

零件

@Singleton
@Component(modules = {AppModule.class, ToastMakerModule.class, SensorControllerModule.class})

public interface AppComponent {
    void inject(MainActivity mainActivity);

    @Component.Builder
    interface Builder {

        AppComponent build();


        // @BindsInstance replaces Builder appModule(AppModule appModule)
        // And removes Constructor with Application AppModule(Application)

        @BindsInstance
        Builder application(Application application);
    }
}
Run Code Online (Sandbox Code Playgroud)

并构建这样的组件

   appComponent = DaggerAppComponent
                .builder()
                .application(this)
                .build();
Run Code Online (Sandbox Code Playgroud)