如何在 android 中使用匕首创建一个对象的多个实例

How to create multiple instances of an object using dagger in android

我使用 dagger 为应用程序创建实例

但由于某些要求,我需要同一对象的多个实例,

任何人都可以提出这样做​​的好方法吗?

您可以使用@Named("") 创建多个实例。在这里,我创建了 2 个具有不同 @Named("")

的拦截器实例

Java:

@InstallIn(SingletonComponent.class)
@Module
class NetworkModule {
    @Provides
    @Singleton
    @Named("headerInterceptor")
    private Interceptor provideInterceptor(UserManager userManager) {
        return new Interceptor() {
            @NonNull
            @Override
            public Response intercept(@NonNull Chain chain) throws IOException {
                return chain.proceed(chain.request());
            }
        };
    }

    @Provides
    @Singleton
    @Named("loggingInterceptor")
    private Interceptor provideLoggingInterceptor() {
        return new LoggingInterceptor.Builder()
                .loggable(BuildConfig.DEBUG)
                .setLevel(Level.BODY)
                .log(Platform.INFO)
                .request("Request")
                .response("Response")
                .build();
    }

    @Provides
    @Singleton
    private OkHttpClient provideOkHttpClient(
            Cache cache,
            @Named("headerInterceptor") Interceptor interceptor,
            @Named("loggingInterceptor") Interceptor loggingInterceptor
    ) {
        return new OkHttpClient.Builder()
                .cache(cache)
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .addInterceptor(loggingInterceptor)
                .build();
    }
}

Kotlin

@InstallIn(SingletonComponent::class)
@Module
class NetworkModule {
    @Provides
    @Singleton
    @Named("headerInterceptor")
    fun provideInterceptor(userManager: UserManager): Interceptor {
        return Interceptor { chain ->
            chain.proceed(chain.request())
        }
    }

    @Provides
    @Singleton
    @Named("loggingInterceptor")
    fun provideLoggingInterceptor(): Interceptor {
        return LoggingInterceptor.Builder()
            .loggable(BuildConfig.DEBUG)
            .setLevel(Level.BODY)
            .log(Platform.INFO)
            .request("Request")
            .response("Response")
            .build()
    }

    @Provides
    @Singleton
    fun provideOkHttpClient(
        cache: Cache,
        @Named("headerInterceptor") interceptor: Interceptor,
        @Named("loggingInterceptor") loggingInterceptor: Interceptor
    ): OkHttpClient {
        return OkHttpClient.Builder()
            .cache(cache)
            .connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(interceptor)
            .addInterceptor(loggingInterceptor)
            .build()
    }
}

您可以使用@Named 注释来实现此行为,

示例:

第一步: 使用带有唯一键的@Named 注释声明您的方法

@Provides
@Singleton
@Named("cached")
fun someInstance() = return someInstance()

第 2 步: 使用带有声明键的实例

@Inject @Named("cached")
val instance: someInstance