简体   繁体   English

使用 hilt-android 在 Singleton 实例进行依赖注入

[英]Dependency injection at Singleton instance with hilt-android

Have a question while using hilt-android.使用 hilt-android 时遇到问题。
And I'm making some.network module with retrofit, and handling with hilt.我正在用 retrofit 制作 some.network 模块,并用刀柄处理。

@InstallIn(SingletonComponent::class)
object NetworkModule {    

    @Singleton
    @Provides
    fun provideBaseRetrofit(
      @BaseHttpClient
      client: OkHttpClient): Retrofit {
        val builder =
          Retrofit.Builder()
            .baseUrl(URL.Server.baseURL)
            .addCallAdapterFactory(ApiResponseCallAdapterFactory.create())
            .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
            .client(client)
            .build()
        
        return builder
    }

    @BaseHttpClient
    @Provides
    fun provideDefaultClient(
      @BaseRequestIntercept
      requestInterceptor: Interceptor,
      @BaseResponse
      responseInterceptor: Interceptor,
      @Logging
      logging: Interceptor): OkHttpClient {
        return DefaultHttpClient().newBuilder().apply {
            addInterceptor(requestInterceptor)
            addInterceptor(responseInterceptor)
            addInterceptor(logging)
        }.build()
    }
}

@Module
@InstallIn(SingletonComponent::class)
abstract class InterceptorModules {
    @BaseRequestIntercept
    @Binds
    abstract fun bindRequestInterceptor(request: RequestInterceptor): Interceptor
    
    @BaseResponse
    @Binds
    abstract fun bindResponseInterceptor(response: ResponseInterceptor): Interceptor
}

At this point, my question is, @singleton annotation.在这一点上,我的问题是, @singleton注释。

At first, I attach singleton annotation to every single object, such as httpClient/requestInterceptor/responseInterceptor首先,我将 singleton 注释附加到每个 object,例如httpClient/requestInterceptor/responseInterceptor

However, the object's hashcode doesn't change even I remove annotation.但是,即使我删除注释,对象的hashcode也不会改变。
(Except retrofit, I remain singleton annotation to retrofit ) (除了retrofit,我保留singleton注解为retrofit

And my assume is, the singleton annotation only needs most upper instance.我的假设是,singleton 注释只需要最上面的实例。
like...喜欢...

Intercept(not singleton) -> okhttp(not singleton) -> retrofit (singleton)拦截(非单例)-> okhttp(非单例)-> retrofit(单例)

Why I though like this, retrofit is a singleton instance.为什么我喜欢这样,retrofit 是一个 singleton 实例。 Instance will not change so that It'll not make any other dependency object while application is living.实例不会改变,因此它不会在应用程序运行时产生任何其他依赖 object 。

But I have no idea that my assume is correct, so I ask you guys...但我不知道我的假设是否正确,所以我问你们......

Every example what I refer to, always attach @singleton annotation to every single provides class..我所指的每个示例,始终将 @singleton 注释附加到每个提供 class..

Is my approach wrong?我的方法错了吗?

Thanks in advance!!!提前致谢!!!

Your assumption is correct.你的假设是正确的。 Since Retrofit is Singleton and Retrofit is the only thing that needs Intercept / OkHttp there is only one instantiation of those.由于RetrofitSingleton ,而Retrofit是唯一需要Intercept / OkHttp的东西,因此只有一个实例。 You can even test it the other way around - have everything else @Singleton and then you would see that Intercept / OkHttp hashCode is the same but Retrofit changes.你甚至可以用另一种方式测试它 - 拥有其他一切@Singleton然后你会看到Intercept / OkHttp hashCode是相同的但Retrofit发生了变化。

To answer the underlying question - should every component there( Intercept / OkHttp ) have @Singleton ?要回答基本问题 - 那里的每个组件( Intercept / OkHttp )都应该有@Singleton吗?

In general, it depends only on what you need it to do.一般来说,它只取决于你需要它做什么。 Here you are not doing a mistake having or not having it there because Retrofit already takes care of it and it works but... this way you are implicitly relying on them being scoped by some other dependency which could cause issues in the future where you would count them as singleton even though they are not defined/scoped as such.在这里你没有犯错,因为Retrofit已经处理了它并且它可以工作但是......这样你就隐含地依赖它们被其他依赖性所限制,这可能会导致你将来出现问题会将它们算作 singleton,即使它们没有这样定义/限定范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM