有使用Guice的子类的公共提供程序吗? [英] Have a common provider for subclasses using Guice?
问题描述
我正在尝试为所有子类实现一个公共提供程序,设想一些模式:
SuperComponent.class
是的父级ComponentA.class
和 ComponentB.class
。
我有提供者:
I am trying to implement a common provider for all the subclasses, imagine some schema:
SuperComponent.class
is the parent of ComponentA.class
and ComponentB.class
.
I have the provider:
@Provides
<T extends SuperComponent> List<T> providesComponents(Provider<T> provider) {
List<T> componentList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
componentList.add(provider.get());
}
return componentList;
}
这个想法是在需要对象<$ c $时调用此提供程序c> List< ComponentA> 和/或 List< ComponentB>
。
想象这样的事情:
The idea is to call this provider when is required the objects List<ComponentA>
and/or List<ComponentB>
in another class' constructor.
Imagine something like:
public class ResourceManager {
List<ComponentA> componentAList;
List<ComponentB> componentBList;
@Inject
public ResourceManager(List<ComponentA> componentAList, List<ComponentB> componentBList) {
this.componentAList = componentAList;
this.componentBList = componentBList;
}
我收到一条错误消息:
1) com.google.inject.Provider<T> cannot be used as a key; It is not fully specified.
我如何使其工作?我知道我可以为 List< ComponentA>
和 List< ComponentB>
中的每一个创建不同的提供程序,但是我需要它,因为实际上组件的数量远远大于2 ...
How can I make it work? I know I can do it creating different providers for each of the List<ComponentA>
and List <ComponentB>
but I need it as in the reality the number of components is much larger than 2...
推荐答案
我不认为有一种很好的内置方法来处理-Guice可以绑定很多东西,或者检查和操纵其绑定,但是没有良好的接口来创建元级绑定。但是,您确实有一些选择:
I don't think there's a good built-in way to handle this--Guice can bind a lot, or inspect and manipulate its bindings, but does not have good interfaces to create meta-level bindings. You do have a few options, though:
-
通过提供者将列表构造留给消费者:
Leave the List-constructing to the consumer, through Providers:
public static <T> List<T> createList(Provider<T> provider) {
List<T> list = new ArrayList<T>();
for (int i = 0; i < 5; i++) {
list.add(provider.get());
}
return list;
}
@Inject MyConsumer(Provider<Foo> depProvider) {
List<Foo> myFoos = createList(depProvider);
}
列出需要绑定的类并创建配置
方法中的提供程序:
public class MyModule extends AbstractModule {
public void configure() {
List<Class<?>> classList = Lists.newArrayList(Class1.class, Class2.class);
for (Class<?> clazz : classList) {
bind(listOf(clazz)).toProvider(new ComponentListProvider<?>(getProvider(clazz)));
}
}
private static <T> Key<List<T>> listOf(Class<T> clazz) {
return new Key<List<T>>() {};
}
private static class ComponentListProvider<T> implements Provider<List<T>>() {
private final Provider<T> wrappedProvider;
ComponentListProvider(Provider<T> wrappedProvider) {
this.wrappedProvider = wrappedProvider;
}
@Override public List<T> get() {
return createList(wrappedProvider);
}
}
}
这使用 getProvider
,一种非常方便的方法,该方法检索类型化的 Provider
,该类型将在 Injector
已创建(但不早于此)。
This uses getProvider
, an extremely handy method that retrieves a typed Provider
that will work as soon as the Injector
is created (but not before).
编写一个模块,该模块通过遍历in中的每个绑定来执行上述操作之一使用 Guise SPI 。
Write a module that does one of the above by iterating across every binding in the Module using Guice SPI.
希望有帮助!
这篇关于有使用Guice的子类的公共提供程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!