我们正在尝试将我们的Web框架Wicket(1.4.x)与Dagger的依赖注入集成在一起。为此,我们创建了一个Wicket组件注入器:
class DaggerComponentInjector
extends ConfigurableInjector
implements IComponentInstantiationListener {
...
@Override
public Object inject(Object object, IFieldValueFactory factory) {
if (!AppModule.canInject(object.getClass())) {
return object;
}
final DaggerGraphHolder holder = Application.get().getMetaData(DaggerGraphHolder.GRAPH_KEY);
holder.getGraph().inject(object);
return object;
}
@Override
public void onInstantiation(Component component) {
inject(component);
}
...
它检查对象的类是否在任何地方包含@Inject,然后调用Dagger来注入依赖项。
不幸的是,这不起作用:
public class LogoutPage extends WebPage {
@Inject
private AuthenticationService authenticationService;
...
}
在我们的注销页面中注入了一项服务。但是当我们导航到页面时会发生这种情况:
Caused by: java.lang.IllegalArgumentException: No inject registered for members/com.myapp.LogoutPage. You must explicitly add it to the 'injects' option in one of your modules.
at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:309)
at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:290)
我们有数百个这样的课程。
如何在不手动将所有类添加到@Module注入数组(或创建提供程序方法)的情况下解决此问题?