刀柄匕首寄存器工厂类

时间:2020-10-09 15:51:43

标签: android dependency-injection factory dagger-hilt

如何在Hilt-Dagger中注册具有构造函数依赖项的工厂类?

示例:一个类提供各种服务,并且需要依赖。

注意:框架应使用此类提供IApiServiceX的实例,解决其对Retrofit的依赖关系并管理其生存期。

public class ApiServices {

    private final Retrofit retrofit;

    @Inject
    public ApiServices(@NonNull Retrofit retrofit) {
        this.retrofit = retrofit;
    }

    @Provides
    public @NonNull IApiService1 getApiService1() {
        return this.retrofit.create(IApiService1.class);
    }

    @Provides
    public @NonNull IApiService2 getApiService2() {
        return this.retrofit.create(IApiService2.class);
    }

    @Provides
    public @NonNull IApiServiceN getApiServiceN() {
        return this.retrofit.create(IApiServiceN.class);
    }
}

0 个答案:

没有答案