Java 如何使用Guice检索给定接口的多绑定实现集
我的应用程序是用Guice构建的。应用程序模块使用多绑定器绑定接口的多个实现Java 如何使用Guice检索给定接口的多绑定实现集,java,guice,Java,Guice,我的应用程序是用Guice构建的。应用程序模块使用多绑定器绑定接口的多个实现 Multibinder<FooInterface> newSetBinder = Multibinder.newSetBinder(binder(), FooInterface.class); newSetBinder.addBinding().to(FooInterfaceImpl.class); newSetBinder.addBinding().to(BarInterfaceImpl.class);
Multibinder<FooInterface> newSetBinder = Multibinder.newSetBinder(binder(), FooInterface.class);
newSetBinder.addBinding().to(FooInterfaceImpl.class);
newSetBinder.addBinding().to(BarInterfaceImpl.class);
但是,这不适用于多绑定接口。我想返回给定接口的绑定集合,并断言该集合包含预期的类
有什么方法可以做到这一点吗?使用以下方法找到了答案:
Injector.findBindingsByType(TypeLiteral<T> type)
…在返回的
列表中,我可以断言预期的类是其中一个绑定的目标。使用以下方法找到了答案:
Injector.findBindingsByType(TypeLiteral<T> type)
…在返回的列表中,我可以断言预期的类是其中一个绑定的目标
injector.findBindingsByType(new TypeLiteral<FooInterface>() {}))