提供单例依赖与对象初始化

时间:2019-11-26 12:17:09

标签: java android dagger-2

我有两个关于Dagger2的问题,它是Singleton的范围

我使用了RetrofitGSON,但这仅是示例,我想确保自己正确理解Dagger2

我有NetowrkModule,例如:

@Module
public class NetworkModule {

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
        return new Retrofit.Builder()
                .addConverterFactory(gsonConverterFactory)
                .baseUrl("https://example.com")
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @Singleton
    @Provides
    OkHttpClient provideOkHttpClient() {
        return new OkHttpClient.Builder().build();
    }

    @Singleton
    @Provides
    GsonConverterFactory provideGson() {
        return GsonConverterFactory.create();
    }
}

ApplicationComponent

@Singleton
@Component(modules = {
        AndroidSupportInjectionModule.class,
        ApplicationModule.class,
        ViewBuildersModule.class,
        AuthServiceModule.class
})

public interface ApplicationComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(MyApplication application);
        ApplicationComponent build();
    }
    void inject(MyApplication app);
}
  1. 我也应该将OkHttpClient提供者注释为单例吗? (我需要Retrofit实例在应用程序范围内是单例的。)
  2. 在翻新提供程序中提供GsonConverterFactory VS创建GsonConverterFactory有什么区别?

    @Module
    public class NetworkModule {
    
        @Provides
        @Singleton
        Retrofit provideRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
            return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("https://example.com")
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        }
    
        @Singleton
        @Provides
        OkHttpClient provideOkHttpClient() {
            return new OkHttpClient.Builder().build();
        }
    }
    

1 个答案:

答案 0 :(得分:1)

1。我也应该将OkHttpClient提供程序注释为单例吗? (我需要Retrofit实例在应用程序范围内是单例的。)

将okhttpclient绑定标记为单例将确保系统中需要okhttpclient绑定的任何绑定都将接收相同的绑定实例。如果未将其标记为单例,则依赖于okhttpclient的每个绑定都将获得okhttpclient的单独实例。例如,假设您创建一个还需要okhttpclient的RestClient。

@Singleton
@Provides
RestClient provideRestClient(OkHttpClient okhttpclient) {
    return RestClient(okhttpClient);
}

@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
    return new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl("https://example.com")
        .client(okHttpClient)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();
}

@Singleton // marked as singleton
@Provides
OkHttpClient provideOkHttpClient() {
    return new OkHttpClient.Builder().build();
}

在上述情况下,改造和restclient都将收到相同的Okhttpclient实例。

但是,如果您从okhttpclient绑定中删除@Singleton批注,那么restclient和翻新版将收到不同的okhttpclient绑定实例。

2。提供GsonConverterFactory VS在改造提供程序中创建GsonConverterFactory与有什么区别?

同样,根据所需图形的绑定范围可以看到差异。 提供GsonConverterFactory 会显示为图形中所有人的绑定,而 reate GsonConverterFactory 不会。