在这个问题中我谈论Dagger2 。 Dagger2 基本上由组件和模块组成。这是一个例子:
假设我有一个界面:
public interface MyCoolService {
void run();
}
以及可能的实现:
public class MyCoolServiceImpl {
@Override
void run() {}
}
我可以使用 Dagger2 生成将实现与接口(interface)链接起来:
@Component(modules = {MyModule.class})
@Singleton
public interface Component {
MyCoolService getMyCoolService();
}
和
@Module
public class MyModule {
@Provides @Singleton
MyCoolService provideMyCoolService() {
return new MyCoolServiceImpl();
}
}
这是 Dagger2 的简要介绍。现在假设我有以下界面:
public interface MySecondCoolService {
void doCoolStuff();
}
没有实现MySecondCoolServiceImpl
的MySecondCoolService
在代码中。相反,我有一个注释 @JustForCoolStuff
标记字段和方法。我创建了一个注释处理器,它收集所有这些注释并生成 MySecondCoolServiceImpl
它实现了 MySecondCoolService
.
编译器知道新接口(interface) MySecondCoolService
在注释处理器运行之前。所以我可以将我的组件更改为:
@Component(modules = {MyModule.class})
@Singleton
public interface Component {
MyCoolService getMyCoolService();
MySecondCoolService getMySecondCoolService();
}
问题是我在代码中还没有实现,并且我不知道 MySecondCoolService
的实现名称它将由注释处理器生成。因此,我无法在 MyModule
中使用正确的实现来连接接口(interface)。 。我能做的就是更改我的注释处理器,以便它为我生成一个新模块。我的注释处理器可以生成一个模块( MyGeneratedModule
),如下所示:
@Module
public class MyGeneratedModule {
@Provides @Singleton
MySecondCoolService provide MySecondCoolService() {
return new MySecondCoolServiceImpl();
}
}
再次MyGeneratedModule
由注释处理器生成。在运行注释处理器之前我无法访问它,而且我也不知道它的名称。
问题是这样的:注释处理器必须以某种方式告诉 Dagger2 有一个 Dagger2 应该考虑的新模块。由于注释处理器无法更改文件,因此无法扩展 @Component(modules = {MyModule.class})
注释并将其更改为这样的内容:@Component(modules = {MyModule.class, MyGeneratedModule.class})
有没有办法添加 MyGeneratedModule
以编程方式连接到 dagger2 依赖图?我的注释处理器如何告诉 Dagger2 在接口(interface)和实现之间应该有一个新的连接,正如我上面所描述的?
进军: 我知道类似的事情可以在 Google Guice 中完成和 Google Gin 。这样做的项目是 GWTP 。那里有一个演示者:
public class StartPagePresenter extends ... {
@NameToken("start")
public interface MyProxy extends ProxyPlace<StartPagePresenter> {
}
...
}
其中有 @NameToken
ProxyPlace
的注释界面。在你的AbstractPresenterModule
您将 View 与演示者和代理连接起来:
public class ApplicationModule extends AbstractPresenterModule {
bindPresenter(StartPagePresenter.class,
StartPagePresenter.MyView.class, StartPageView.class,
StartPagePresenter.MyProxy.class);
...
}
因此可以看到 MyProxy
没有实现给出了接口(interface)。由生成器创建的实现(类似于注释处理器,但适用于 GWT)。有 Generator 生成 StartPagePresenter.MyProxy
的实现并将其添加到guide/gin系统中:
public class StartPagePresenterMyProxyImpl extends com.gwtplatform.mvp.client.proxy.ProxyPlaceImpl<StartPagePresenter> implements buddyis.mobile.client.app.start.StartPagePresenter.MyProxy, com.gwtplatform.mvp.client.DelayedBind {
private com.gwtplatform.mvp.client.ClientGinjector ginjector;
@Override
public void delayedBind(Ginjector baseGinjector) {
ginjector = (com.gwtplatform.mvp.client.ClientGinjector)baseGinjector;
bind(ginjector.getPlaceManager(),
ginjector.getEventBus());
presenter = new CodeSplitProvider<StartPagePresenter>( ginjector.getbuddyismobileclientappstartStartPagePresenter() );
...
}
}
<小时/>
最佳答案
Is there a way to add MyGeneratedModule programmatically to the dagger2 dependency graph?
是的。在模块中使用构造函数参数。
@Module
public class MyModule {
private final MyCoolService serviceImpl;
public MyModule(MyCoolService serviceImpl) {
this.serviceImpl = serviceImpl;
}
@Provides @Singleton
MyCoolService provideMyCoolService() {
return new MyCoolServiceImpl();
}
}
在构建图表时初始化 serviceImpl 已完成:
DaggerComponent.builder().myModule(new MyModule(serviceImpl)).build();
关于java - 使用注释处理延迟绑定(bind)到 Dagger2 Graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836221/