我最近将匕首2.8更新为2.9匕首。和最新版本的文档已添加如下:

-添加了@BindsInstance以便组件构建器轻松绑定在图形外部构造的实例。

-生产者:添加了ProducerMonitor.ready (),当所有生产者的输入均可用时将调用它。

-删除了@Provides(type =...)用法。请改用dagger.multibindings中的注释。 @Produces.type也已删除。

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

-@Component.dependencies不能再包含@Modules

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

谢谢!!

注意:我是匕首2的新手,但您希望能够最大程度地利用此库。

最佳答案

@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);
    }
}


这些模块(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);
    }

}


像这样构建组件

 appComponent = DaggerAppComponent
                .builder()
                .appModule(new AppModule(this))
                .sensorControllerModule(new SensorControllerModule())
                .toastMakerModule(new ToastMakerModule())
                .build();




使用@BindsInstance

@Module
public class AppModule {

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


零件

@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);
    }
}


并像这样构建组件

   appComponent = DaggerAppComponent
                .builder()
                .application(this)
                .build();

关于dagger-2 - 工作原理@BindsInstance Dagger 2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42081832/

10-12 03:39