Java 如何让已安装的MapBinder向其中添加其他绑定
我使用Java 如何让已安装的MapBinder向其中添加其他绑定,java,guice,Java,Guice,我使用com.google.inject:guice。在我的项目中,我包含了一个依赖项,它有一个模块(扩展了com.google.inject.AbstractModule)定义了一个类似的MapBinder public class ParentGuiceModule extends AbstractModule { @Override protected void configure() { MapBinder.newMapBinder(binder(
com.google.inject:guice
。在我的项目中,我包含了一个依赖项,它有一个模块(扩展了com.google.inject.AbstractModule
)定义了一个类似的MapBinder
public class ParentGuiceModule extends AbstractModule {
@Override
protected void configure() {
MapBinder.newMapBinder(binder(), TypeLiteral.get(String.class), TypeLiteral.get(SomeModuleClass.class));
...
}
}
在我的模块类中,我希望获得MapBinder
,并向其添加新绑定。我的意思是我想写这样的东西:
public class MyGuiceModule extends AbstractModule {
@Override
protected void configure() {
MapBinder<String, SomeModuleClass> parentModules = MapBinder.get(binder(), TypeLiteral.get(String.class), TypeLiteral.get(SomeModuleClass.class));
parentModules.addBinding("MyId").to(MyClass.class);
}
}
公共类MyGuiceModule扩展了AbstractModule{
@凌驾
受保护的void configure(){
MapBinder parentModules=MapBinder.get(binder(),TypeLiteral.get(String.class),TypeLiteral.get(SomeModuleClass.class));
parentModules.addBinding(“MyId”).to(MyClass.class);
}
}
我该怎么做?我无法更改父模块
我查看了
MapBinder
类,似乎它没有任何方法可以安装MapBinder
这正是MapBinder的设计目的毕竟,如果您从单个模块中了解MapBinder中的所有内容,您可以只编写@providesmap
或bind(newtypeliteral(){})
并完成它
从:
支持从不同模块提供映射绑定。例如,可以让CandyModule和ChipsModule都创建自己的MapBinder
,并为地图提供绑定。当该映射被注入时,它将包含来自两个模块的条目
不要因为名称newMapBinder
而气馁:只要您拥有与newMapBinder
完全相同的参数,并且将两个模块安装在同一个注入器中,您将得到一个包含两个模块绑定的映射