[英]@Component.Builder is missing setters for required modules or components [Dagger2]
I'm trying to use Component.Builder to inject my module but something is wrong with RestModule -one of my modules. 我正在尝试使用Component.Builder注入我的模块,但RestModule-我的模块之一出了点问题。 it returns
它返回
Error:(41, 5) error: @Component.Builder is missing setters for required modules or components: [com.injection.module.services.RestModule]
错误:(41,5)错误:@ Component.Builder缺少所需模块或组件的设置器:[com.injection.module.services.RestModule]
Here's the ApplicationComponent : 这是ApplicationComponent :
@Singleton
@Component(modules = {PresenterEnvironmentModule.class,
RestModule.class})
public interface PresenterEnvironmentComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder sharedPreferenceService(SharedPrefService sharedPreferences);
@BindsInstance
Builder loggingService(LoggingService loggingService);
@BindsInstance
Builder applicationService(Application application);
@BindsInstance
Builder connectivityService(ConnectivityService connectivityService);
@BindsInstance
Builder schedulersService(SchedulersService schedulersService);
@BindsInstance
Builder sessionService(SessionService sessionService);
PresenterEnvironmentComponent build();
}
@NonNull
PresenterEnvironment presenterEnvironment();
void inject(BaseBootstrapActivity<BaseBootstrapPresenter> baseBootstrapActivity);
}
And Here's The RestModule: 这是RestModule:
@Module
public class RestModule {
private final boolean debuggable;
private final String baseUrl;
@NonNull
private final TokenInterceptor tokenInterceptor;
@NonNull
private final BootstrapRestService bootstrapRestService;
@Nullable
private Interceptor[] additionalInterceptors;
@Nullable
private Function<OkHttpClient.Builder, OkHttpClient.Builder> onOkHttpClient;
@Nullable
private Function<OkHttpClient.Builder, OkHttpClient.Builder> onOkHttpClientForUploading;
public RestModule(@NonNull RestSettings restSettings) {
this.debuggable = restSettings.isDebuggable();
this.baseUrl = restSettings.getBaseUrl();
this.tokenInterceptor = restSettings.getTokenInterceptor();
this.bootstrapRestService = restSettings.getRestService();
this.onOkHttpClient = restSettings.getOnOkHttpClient();
this.onOkHttpClientForUploading = restSettings.getOnOkHttpClientForUploading();
this.additionalInterceptors = restSettings.getAdditionalInterceptors();
}
@Provides
@Singleton
@NonNull
@Type.Basic
public OkHttpClient provideOkHttpClient(@NonNull TokenInterceptor tokenInterceptor,
@NonNull HttpLoggingInterceptor loggingInterceptor,
@NonNull OkLogInterceptor okLogInterceptor,
@NonNull CachingInterceptor cacheInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(tokenInterceptor);
if (additionalInterceptors != null && additionalInterceptors.length > 0) {
for (Interceptor interceptor : additionalInterceptors) {
builder.addInterceptor(interceptor);
}
}
if (onOkHttpClient != null) {
builder = apply(onOkHttpClient, builder);
}
if (debuggable) {
// Add HttpLoggingInterceptor
builder.addInterceptor(loggingInterceptor);
// add cache interceptor
builder.addNetworkInterceptor(cacheInterceptor);
// add okLog interceptor
builder.addInterceptor(okLogInterceptor);
}
return builder.build();
}
@Provides
@Singleton
@NonNull
@Type.ForUploading
public OkHttpClient provideOkHttpClientForUploading(@NonNull TokenInterceptor tokenInterceptor,
@NonNull HttpLoggingInterceptor loggingInterceptor,
@NonNull OkLogInterceptor okLogInterceptor,
@NonNull CachingInterceptor cacheInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(tokenInterceptor);
if (additionalInterceptors != null && additionalInterceptors.length > 0) {
for (Interceptor interceptor : additionalInterceptors) {
builder.addInterceptor(interceptor);
}
}
if (onOkHttpClientForUploading == null) {
builder.writeTimeout(60, TimeUnit.SECONDS);
builder.connectTimeout(60, TimeUnit.SECONDS);
builder.readTimeout(60, TimeUnit.SECONDS);
} else {
builder = apply(onOkHttpClientForUploading, builder);
}
if (debuggable) {
// Add HttpLoggingInterceptor
builder.addInterceptor(loggingInterceptor);
// add cache interceptor
builder.addNetworkInterceptor(cacheInterceptor);
// add okLog interceptor
builder.addInterceptor(okLogInterceptor);
}
return builder.build();
}
@Provides
@Singleton
@NonNull
public OkLogInterceptor provideOkLogInterceptor() {
return new OkLogInterceptor.Builder().withAllLogData().build();
}
@Provides
@Singleton
@NonNull
public HttpLoggingInterceptor provideHttpLoggingInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
@Provides
@Singleton
@NonNull
@Type.Basic
public Retrofit provideRetrofit(@NonNull @Type.Basic OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(LoganSquareConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
@Provides
@Singleton
@NonNull
@Type.ForUploading
public Retrofit provideRetrofitForUploading(@NonNull @Type.ForUploading OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(LoganSquareConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
@Provides
@Singleton
@NonNull
public CachingInterceptor provideCachingInterceptor(LoggingService loggingService) {
return new CachingInterceptor(loggingService);
}
@Provides
@Singleton
@NonNull
public TokenInterceptor provideTokenInterceptor() {
return tokenInterceptor;
}
@Provides
@Singleton
@NonNull
public BootstrapRestService provideBaseRestService(@NonNull @Type.Basic Retrofit retrofit,
@NonNull @Type.ForUploading Retrofit retrofitForUploading) {
return bootstrapRestService.setup(retrofit, retrofitForUploading);
}
@NonNull
private <T, R> R apply(@NonNull Function<T, R> f, @NonNull T t) {
try {
return Preconditions.checkNotNull(f.apply(t));
} catch (Throwable ex) {
throw new IllegalStateException("Applying function for " + t.toString() + " Threw " + ex);
}
}
}
@Module
public class RestModule {
// ...
public RestModule(@NonNull RestSettings restSettings) {
RestModule is an instantiable Module without a visible parameterless constructor. RestModule是一个可实例化的模块,没有可见的无参数构造函数。 Dagger doesn't know how to create an instance of it.
Dagger不知道如何创建它的实例。 You'll need to supply a method like
Builder restModule(RestModule restModule)
on the @Component.Builder, and call it during creation like your @BindsInstance
methods. 您需要在@ Component.Builder上提供类似于
Builder restModule(RestModule restModule)
的方法,并在创建过程中像@BindsInstance
方法一样调用它。
Each module dependency that Dagger can't instantiate itself (eg, the module doesn't have a visible no-args constructor) must have an abstract setter method.
Dagger无法实例化的每个模块依赖项(例如,该模块没有可见的无参数构造函数)必须具有抽象的setter方法。 Other module dependencies (ones that Dagger can instantiate) are allowed, but not required.
允许其他模块依赖性(Dagger可以实例化的依赖性),但不是必需的。 (from the @Component.Builder javadoc )
(来自@ Component.Builder javadoc )
Not all Modules are instantiable; 并非所有模块都是实例化的。 some are interfaces or abstract classes.
一些是接口或抽象类。 Because Dagger calls static methods statically, and reflects on
@Binds
methods at compile time, many modules do not need to be instantiated at all. 由于Dagger静态调用静态方法,并在编译时反映
@Binds
方法,因此许多模块根本不需要实例化。 This is often more optimal, as Dagger can skip creating an instance. 这通常是最佳选择,因为Dagger可以跳过创建实例。
For Modules that are instantiable but have visible zero-arg constructors, Dagger can automatically call new
, and you may leave those Modules off the Builder. 对于可实例化但具有可见的零参数构造函数的模块,Dagger可以自动调用
new
,您可以将这些模块保留在构建器之外。 (You may choose to include them; they don't need to be called, but may be useful if you subclass modules, which the docs advise against anyway .) (您可以选择包含它们;它们不需要被调用,但是如果您将模块归类,则可能会很有用,但是文档建议不要这样做。)
For Modules that are instantiable but only have constructors with arguments, Dagger cannot create the Modules; 对于可实例化但仅具有带参数的构造函数的模块,Dagger无法创建模块。 it will not supply the constructor with arguments from the graph as it does with
@Provides
methods. 它不会像
@Provides
方法那样向构造函数提供来自图的参数。 You'll need to explicitly create a Builder method, and call it with an instance of a Module that you create yourself. 您需要显式创建一个Builder方法,并使用您自己创建的Module的实例进行调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.