- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道有很多类似的问题,但我仍然无法找到解决问题的方法,在这个阶段,我没有想法。我有以下设置:
代码是这样的:
ApplicationModule.java
@Module
public class ApplicationModule {
private Application mApp;
public ApplicationModule(Application app) {
mApp = app;
}
@Provides
@Singleton
public Application provideApplication() {
return mApp;
}
@Provides
@Singleton
Context provideApplicationContext() {
return mApp.getApplicationContext();
}
}
ApplicationComponent.java
@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
Application application();
Context getContext();
}
NetModule.java
@Module
public class NetModule {
String mBaseUrl;
public NetModule(String baseUrl) {
this.mBaseUrl = baseUrl;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
return new Cache(application.getCacheDir(), cacheSize);
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient client = new OkHttpClient();
client.setCache(cache);
return client;
}
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(JacksonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
}
@Provides
@Singleton
PlaceRequests providePlaceRequests(Retrofit retrofit) {
return retrofit.create(PlaceRequests.class);
}
}
NetComponent.java
@Singleton
@Component(
dependencies = {ApplicationModule.class},
modules = {NetModule.class}
)
public interface NetComponent {
Application application();
PlaceRequests getPlaceRequests();
}
CityModule.java
@Module
public class CityModule {
private CityMvp.View view;
public CityModule(CityMvp.View view) {
this.view = view;
}
@Provides
public CityMvp.View provideView() {
return view;
}
@Provides
public CityMvp.Interactor provideInteractor(PlaceRequests placeRequests) {
return new CityInteractor(placeRequests);
}
@Provides
public CityPresenter providePresenter(CityMvp.View cityView, CityMvp.Interactor interactor) {
return new CityPresenter(cityView, interactor);
}
}
CityComponent.java
@PerFragment
@Component(
dependencies = {NetModule.class},
modules = {CityModule.class}
)
public interface CityComponent {
void inject(CityFragment cityFragment);
}
CityInteractor.java(无法注入(inject)导致 PlaceRequests 依赖)
public class CityInteractor implements CityMvp.Interactor {
private PlaceRequests placeRequests;
public CityInteractor(PlaceRequests placeRequests) {
this.placeRequests = placeRequests;
}
@Override
public void getPlaceDetails(String placeId, String key, Subscriber<PlaceDetails> subscriber) {
Observable<PlaceDetails> observable = placeRequests.getPlaceDetails(placeId, key);
observable.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(subscriber);
}
}
最后是错误轨迹:
: error: com.blabla.blabla.webapi.place.PlaceRequests cannot be provided without an @Provides-annotated method.
void inject(CityFragment cityFragment);
^
com.blabla.blabla.screens.city.CityFragment.presenter
[injected field of type: com.blabla.blabla.screens.city.CityPresenter presenter]
com.blabla.blabla.di.modules.CityModule.providePresenter(com.blabla.blabla.screens.city.CityMvp.View cityView, com.blabla.blabla.screens.city.CityMvp.Interactor interactor)
[parameter: com.blabla.blabla.screens.city.CityMvp.Interactor interactor]
com.blabla.blabla.di.modules.CityModule.provideInteractor(com.blabla.blabla.webapi.place.PlaceRequests placeRequests)
[parameter: com.blabla.blabla.webapi.place.PlaceRequests placeRequests]
3 errors
:app:compileDebugJavaWithJavac FAILED
FAILURE: Build failed with an exception.
据我所知,我在 NetComponent.java 中公开了 PlaceRequests 对象,而 NetModule.java 是 CityComponent 的依赖项。为什么我无法从 CityComponent 获取 PlaceRequests 依赖项?我错过了什么?谢谢!
最佳答案
Component dependencies通过 @Component(dependencies={...})
指定的不应该是模块,因为您拥有它们。它们应该是通过称为提供方法的零参数方法提供依赖项的类型(通常是组件)。
将您的依赖项切换到组件而不是模块。
如果有帮助,您可能想要改变您对组件、模块和组件依赖性的看法。您可以将 Dagger 视为创建对象图,其中模块定义图的输入或绑定(bind),组件定义图的输出或消费者。这使得组件依赖性成为类型或组件包含或从另一个外部源导入,这可能包含不同的 Dagger 创建的组件,但可能不包含模块——而不是依赖于您将依赖于使用该模块的组件的模块。
关于java - 没有 @Provides-annotated 方法就不能提供 Dagger 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41533745/
我的问题是 this one. 的一个分支 我有一个要验证的注释(比如电话注释)。我可以使用@phone 验证器来检查电话对象是否有效。我还希望能够将此验证器放在包含电话的联系信息对象上。有没有一种方
我的问题是 this one. 的一个分支 我有一个要验证的注释(比如电话注释)。我可以使用@phone 验证器来检查电话对象是否有效。我还希望能够将此验证器放在包含电话的联系信息对象上。有没有一种方
例如 class LoggingService [Inject] (protected val logger: Logger) class LoggingService @Inject (protec
你觉得你是java高手吗? 您是否深谙反射 API 的 secret ? public @interface @a {} public @interface @b {} @Mark public @i
我对 Spring 和 JUnit 非常陌生。我正在尝试为 spring 服务类运行一个简单的 JUnit 测试用例,但它失败了,我得到了这个异常。我还没有编写任何测试,但在实现之前尝试运行。使用to
对于spring和JUnit来说是非常新的东西。 我正在尝试为spring服务类运行一个简单的JUnit测试用例,但是失败了,并且出现了这个异常。我还没有编写任何测试,但是尝试在实现之前进行测试。 使
我有一个实体Test,它将从特征中获取它的属性(和基本方法): class Test { use Trait_title; } trait Trait_title{ /** *
我(当然)正在尝试使用许多我不太了解的构造来维护一个项目。在尝试弄清楚 Spring 中 AOP 使用的过程中,我遇到了带有以下注释的方法: @Around(value = "@annotation(
目前我正在编写一个注释处理器,它将生成新的源代码。该处理器与应用程序本身隔离,因为它是构建项目的一个步骤,我将整个构建系统与应用程序分开。 这就是问题开始的地方,因为我想处理在应用程序中创建的注释。我
我将 Vertx Service Gen 注释处理器与 Kotlin kapt 结合使用。 在注释处理器启动之前,我的 kapt 失败,到处都是以下异常消息: error: scoping const
我很难弄清楚如何从其实际实现类中获取对 java.lang.annotation.Annotation 的引用。 注释本身看起来像这样(来自框架): @Target({ElementType.TYPE
如何创建类似的注释 @Table(name="batch", uniqueConstraints= @UniqueConstraint(columnNames = {"compound_id"
我刚开始使用Spring Boot,我收到这个错误已经有一段时间了,不幸的是无法修复它。从那时起,我一直在谷歌上搜索,但仍然找不到我做错了什么。在我的代码下面找到:。实体。刀。主要。误差率。启动App
输出文本: Execution failed for task ':app:checkDebugDuplicateClasses'. 1 exception was raised by worker
假设我想使用宏注释来复制@specialized(Int) 之类的注释——我知道这很疯狂。像这样的东西: class expand(expanded: Any*) extends Annotation
假设我想使用宏注释来复制@specialized(Int) 之类的注释——我知道这很疯狂。像这样的东西: class expand(expanded: Any*) extends Annotation
注解处理过程中我目前正在处理一个方法的注解: @Override public boolean process(Set elements, RoundEnvironment env) { Mess
我有接口(interface)资源和几个实现它的类,例如音频、视频...此外,我创建了自定义注释MyAnnotation: @MyAnnotation(type = Audio.class) cl
我的项目包括较旧的未注释 Controller 和较新的基于注释的 Controller 。 我使用的是最新的 Spring jar (3.0.5),在我的 dispatcher-servlet.xm
我正在写一些简单的 win32 东西,我正在使用以下 wWinMain int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
我是一名优秀的程序员,十分优秀!