Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何消除剑柄依赖注入周期_Android_Dependency Injection_Retrofit2_Circular Dependency_Dagger Hilt - Fatal编程技术网

Android 如何消除剑柄依赖注入周期

Android 如何消除剑柄依赖注入周期,android,dependency-injection,retrofit2,circular-dependency,dagger-hilt,Android,Dependency Injection,Retrofit2,Circular Dependency,Dagger Hilt,我有一个网络模块类,它提供了一个ApiService实例。 有一个Authenticator类,它在访问令牌过期时刷新该令牌。 验证器需要ApiService实例来进行API调用。 这会导致循环依赖。如何避免这种情况 现在,我正在tokenexpiryaauthenticator内部创建一个新的ApiService类来进行API调用,以打破循环依赖性。 如何正确地将ApiService注入TokenExpiryAuthenticator而不引起循环依赖性 @InstallIn(Singleton

我有一个网络模块类,它提供了一个
ApiService
实例。 有一个
Authenticator
类,它在访问令牌过期时刷新该令牌。 验证器需要
ApiService
实例来进行API调用。 这会导致循环依赖。如何避免这种情况

现在,我正在
tokenexpiryaauthenticator
内部创建一个新的
ApiService
类来进行API调用,以打破循环依赖性。 如何正确地将
ApiService
注入
TokenExpiryAuthenticator
而不引起循环依赖性

@InstallIn(SingletonComponent::class)
@Module
object NetworkModule {

@Provides
@Singleton
@Named("Other")
fun provideRetrofitWithoutInterceptor(@Named("Other") client: OkHttpClient, gson: Gson): Retrofit {
    return Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build()
}

@Provides
@Singleton
fun provideRetrofit(client: OkHttpClient, gson: Gson): Retrofit {
    return Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build()
}

@Provides
@Singleton
fun providesOkHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor, supportInterceptor: SupportInterceptor, tokenExpiryAuthenticator: TokenExpiryAuthenticator): OkHttpClient {
    return OkHttpClient.Builder().writeTimeout(1, TimeUnit.MINUTES)
        .readTimeout(1, TimeUnit.MINUTES)
        .callTimeout(1, TimeUnit.MINUTES)
        .addInterceptor(httpLoggingInterceptor)
        .addInterceptor(supportInterceptor)
        .authenticator(tokenExpiryAuthenticator)
        .build()
}

@Named("Other")
@Provides
@Singleton
fun providesOkHttpClientWithoutInterceptor(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
    return OkHttpClient.Builder().writeTimeout(1, TimeUnit.MINUTES)
        .readTimeout(1, TimeUnit.MINUTES)
        .callTimeout(1, TimeUnit.MINUTES)
        .addInterceptor(httpLoggingInterceptor)
        .build()
}

@Provides
@Singleton
fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
    return if (BuildConfig.DEBUG)
        HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY
        }
    else
        HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.NONE
        }
}

@Provides
@Singleton
fun providesGson(): Gson {
    return GsonBuilder().create()
}

@Provides
@Singleton
fun providesRestApiService(retrofit: Retrofit): ApiService {
    return retrofit.create(ApiService::class.java)
}

@Provides
@Singleton
@Named("Other")
fun providesRestApiServiceWithoutInterceptor(@Named("Other") retrofit: Retrofit): ApiService{
    return retrofit.create(ApiService::class.java)
}

}

您可以拆分您的API服务并创建一个新的AuthenticationApi,该API只包括用于验证或刷新访问令牌的端点。此AuthenticationApi是使用没有验证器的OkHttp实例创建的

这样,您的验证器只需要引用这个精简的改型api。
这还保证了在使用身份验证端点时,如果凭据错误,您不会得到HTTP 401身份验证循环。

但是,dagger-hilt中是否有任何解决方法可以克服此问题!。创建新的API服务是最佳实践?您的代码具有循环依赖性。Dagger只是一个将依赖项注入类的工具。即使它有时像魔法一样,也无法解决编码缺陷。我能想到的唯一解决办法是使用惰性依赖项。虽然我会去清洁更简单的方式如上所述,因为这可能会很快变得混乱。