问题描述
我已经开始使用Dagger 2,并且遇到了一个奇怪的问题,对我来说似乎是个错误.
I've started using Dagger 2 and faced strange issue that looks like a bug to me.
我有3个模块,这些模块组成一个子组件,这些子组件又扩展/增加了更高级别的组件.
I have 3 modules, that are composed into one subcomponent, which in turn extends/pluses higher level component.
子组件非常简单:只需组合模块和一个注入点即可:
Subcomponent is pretty simple: just combination of modules and a single injection point:
@Singleton
@Subcomponent(
modules = {
NavigationDrawerModule.class,
NavigationListModule.class,
SwitcherModule.class
}
)
public interface NavigationDrawerComponent {
NavigationDrawerFragment inject(NavigationDrawerFragment object);
}
第一个模块看起来像这样-它提供了常规的片段级依赖项:
First modules looks like this - it provides general fragment-level dependencies:
@Module
public class NavigationDrawerModule {
private final Activity activity;
private final View rootView;
private final LoaderManager loaderManager;
public NavigationDrawerModule(Activity activity, View rootView, LoaderManager loaderManager) {
this.activity = activity;
this.rootView = rootView;
this.loaderManager = loaderManager;
}
@Provides @Singleton EventBus provideLocalBus() {
return EventBus.builder().build();
}
@Provides @Singleton View provideViewRoot() {
return rootView;
}
@Provides @Singleton LoaderManager provideLoaderManager() {
return loaderManager;
}
@Provides @Singleton Context provideContext() {
return activity;
}
}
第二个模块如下所示-它为屏幕上的一部分UI提供了演示者/控制器及其依赖项:
Second module looks like this - it provides presenter/controller and their dependencies for a subset of UI on screen:
@Module
public class SwitcherModule {
@Provides SwitchController provideSwitcherController(SwitchControllerImpl impl) {
return impl;
}
@Provides SwitcherView provideSwitcherView(SwitcherViewImpl impl) {
return impl;
}
}
第三模块-UI子集的另一个演示者/控制器:
Third module - another presenter/controller for a subset of UI:
@Module
public class NavigationListModule {
@Provides @Singleton NavigationListController provideNavigationListController(NavigationListControllerImpl impl) {
return impl;
}
@Provides @Singleton NavigationListView provideNavigationListView(NavigationListViewImpl impl) {
return impl;
}
}
正在注入的片段的相关部分:
Relevant part of the fragment that is being injected:
@Inject SwitchController identitySwitchController;
@Inject SwitcherView identitySwitcherView;
@Inject NavigationListController navigationListController;
@Inject NavigationListView navigationListView;
NavigationListControllerImpl
实现以下构造函数:
@Inject
public NavigationListControllerImpl(Context ctx, EventBus bus) {
this.ctx = ctx;
this.bus = bus;
}
我从Dagger 2编译器收到的错误如下:
Error I'm getting from the Dagger 2 compiler is the following:
error: ...sidenavigation.navigationlist.NavigationListControllerImpl cannot be provided without an @Inject constructor or from an @Provides-annotated method.
...sidenavigation.NavigationDrawerFragment.navigationListController
[injected field of type: ...sidenavigation.navigationlist.NavigationListController navigationListController]
...sidenavigation.navigationlist.NavigationListModule.provideNavigationListController(...sidenavigation.navigationlist.NavigationListControllerImpl impl)
[parameter: ...sidenavigation.navigationlist.NavigationListControllerImpl impl]
Error抱怨缺少@Inject注释的构造函数,但是它存在!如果我将隐式NavigationListControllerImpl实例创建(通过@Provides
-method参数传递)替换为显式(使用new
),则dagger开始抱怨相同的错误,但现在是presenter对象,它是同一模块中的第二个条目,并且等等.
Error complains about missing @Inject-annotated constructor, but it exists! If I replace implicit NavigationListControllerImpl instance creation (passing via @Provides
-method parameter) with explicit (with new
), dagger starts complaining about the same error but now for the presenter object which is the second entry in the same module, and so on.
所有这些情况看起来非常奇怪,我想听听经验丰富的Dagger 2用户(和开发人员?)提供的信息.
All this situation looks very strange, and I'd like to hear some input from more experienced Dagger 2 users (and developers?).
提前谢谢!
推荐答案
好像我已经弄清楚Dagger 2设置有什么问题.在组件和子组件中不能使用相同的作用域.必需 定义子组件的新范围.就我而言,我最终为我的子组件创建了@Screen范围.
Seems like I've figured out what was wrong with my Dagger 2 setup. It's not possible to use the same scope in both component and subcomponents. It's required to define a new scope for subcomponent. In my case I've ended up creating @Screen scope for me subcomponent.
我想说这是Dagger 2中的一个很小但很烦人的缺陷.显然,如果子组件使用父组件扩展,则dagger-compiler会报告有关父组件和子组件相同范围的不错的和可理解的错误.依赖性.但是,如果父组件和子组件共享相同的作用域,则编译器将报告完全误导的错误.
I'd say that this is a small but very annoying defect in Dagger 2. Apparently dagger-compiler reports nice and understandable error about the same scopes in a parent component and child component if child component is extended with a parent component as dependency. But completely misleading error is reported by the compiler if parent component and child subcomponent share the same scope.
谢谢@lukas在这里给我一个提示 https://stackoverflow.com/a/30383088/808313导致问题解决.
Thank you, @lukas, for giving me a hint here https://stackoverflow.com/a/30383088/808313 that led to a problem resolution.
这篇关于匕首2错误:依赖项“在没有@Inject构造函数的情况下无法提供"而实际上用@Inject注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!