Dagger2 在依赖模块中注入 @Named @Provides 的地方?
Dagger2 where inject @Named @Provides in dependent module?
我使用 https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2 的 dagger2 演示。
我想使用缓存和 non_cached 改造调用。我在 NetModule.java
中创建
@Provides @Named("cached")
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cache(cache)
.build();
return okHttpClient;
}
@Provides @Named("non_cached")
@Singleton
OkHttpClient provideOkHttpClientNonCached() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.build();
return okHttpClient;
}
GitHubModule.java 依赖于 NetModule.java。
我的 GitHubComponent.java
@UserScope
@Component(dependencies = NetComponent.class, modules = GitHubModule.class)
public interface GitHubComponent {
void inject(DemoDaggerActivity activity);
}
我的NetComponent.java
@Singleton
@Component(modules={ApplicationModule.class, NetModule.class})
public interface NetComponent {
// downstream components need these exposed
Retrofit retrofit();
OkHttpClient okHttpClient();
SharedPreferences sharedPreferences();
}
在我的 DemoDaggerActivity.java
中注入改造:
@Inject @Named("cached")
OkHttpClient mOkHttpClient;
@Inject
Retrofit mRetrofit;
重建项目后出现错误:
我在哪里可以告诉 Dagger,我想使用缓存或 non_cached 改造?
您有两个同名方法:provideOkHttpClient()
。重命名其中一个,使它们不同。
您的 Retrofit 提供者应该为 OkHttpClient 使用 @Named
注释,例如:
@Provides
@Singleton
public Retrofit provideRetrofit(@Named("cached") OkHttpClient okHttpClient)
{
return new Retrofit.Builder()
.baseUrl("...")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
如果你使用的是kotlin,那么注入named的正确方法是:
@field:[Inject Named("api1")]
.
来源:https://medium.com/@WindRider/correct-usage-of-dagger-2-named-annotation-in-kotlin-8ab17ced6928
我使用 https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2 的 dagger2 演示。 我想使用缓存和 non_cached 改造调用。我在 NetModule.java
中创建@Provides @Named("cached")
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cache(cache)
.build();
return okHttpClient;
}
@Provides @Named("non_cached")
@Singleton
OkHttpClient provideOkHttpClientNonCached() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.build();
return okHttpClient;
}
GitHubModule.java 依赖于 NetModule.java。 我的 GitHubComponent.java
@UserScope
@Component(dependencies = NetComponent.class, modules = GitHubModule.class)
public interface GitHubComponent {
void inject(DemoDaggerActivity activity);
}
我的NetComponent.java
@Singleton
@Component(modules={ApplicationModule.class, NetModule.class})
public interface NetComponent {
// downstream components need these exposed
Retrofit retrofit();
OkHttpClient okHttpClient();
SharedPreferences sharedPreferences();
}
在我的 DemoDaggerActivity.java
中注入改造:
@Inject @Named("cached")
OkHttpClient mOkHttpClient;
@Inject
Retrofit mRetrofit;
重建项目后出现错误:
我在哪里可以告诉 Dagger,我想使用缓存或 non_cached 改造?
您有两个同名方法:provideOkHttpClient()
。重命名其中一个,使它们不同。
您的 Retrofit 提供者应该为 OkHttpClient 使用 @Named
注释,例如:
@Provides
@Singleton
public Retrofit provideRetrofit(@Named("cached") OkHttpClient okHttpClient)
{
return new Retrofit.Builder()
.baseUrl("...")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
如果你使用的是kotlin,那么注入named的正确方法是:
@field:[Inject Named("api1")]
.
来源:https://medium.com/@WindRider/correct-usage-of-dagger-2-named-annotation-in-kotlin-8ab17ced6928