我编写了一个能够创建任何类型的 ListEditor 的工厂:
public interface ListEditorFactory<T, E extends Editor<T>> {
ListEditor<T, E> create(InsertPanel panel);
}
及其实现:
public class ListEditorFactoryImpl<T, E extends Editor<T> & IsWidget> implements ListEditorFactory<T, E> {
private final Provider<E> editorProvider;
@Inject
public ListEditorFactoryImpl(Provider<E> editorProvider) {
this.editorProvider = editorProvider;
}
@Override
public ListEditor<T, E> create(final InsertPanel panel) {
return ListEditor.of(new EditorSource<E>() {
@Override
public E create(int index) {
final E e = editorProvider.get();
panel.add(e.asWidget());
return e;
}
@Override
public void dispose(E e) {
e.asWidget().removeFromParent();
}
@Override
public void setIndex(E editor, int index) {
panel.insert(editor.asWidget(), index);
}
});
}
}
如果我使用 Gin 注入的 Provider 直接实例化工厂,效果会非常好:
ListEditor<NoteDTO, CharacterNoteScreen> notesEditor;
@Inject @UiField(provided = true) FlowPanel notePanel;
@Inject
protected void init(Provider<CharacterNoteScreen> characterNoteProvider) {
final ListEditorFactory<NoteDTO, CharacterNoteScreen> listEditorFactory = new ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen>(characterNoteProvider);
notesEditor = listEditorFactory.create(notePanel);
但是,如果我想像这样注入整个 ListEditorFactory:
@Inject
protected void init(ListEditorFactory<NoteDTO, CharacterNoteScreen> listEditorFactory) {
notesEditor = listEditorFactory.create(notePanel);
杜松子酒抱怨一个简单的bind(ListEditorFactory.class).to(ListEditorFactoryImpl.class);
绑定:
[ERROR] Generator 'com.google.gwt.inject.rebind.GinjectorGenerator' threw an exception while rebinding 'net.test.client.MyGinjector'
com.google.inject.ConfigurationException: Guice configuration errors:
1) javax.inject.Provider<E> cannot be used as a key; It is not fully specified.
我试过了,bind(new TypeLiteral<ListEditorFactory<?, ?>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<?, ?>>() {});
但得到了一个
java.lang.IllegalArgumentException: Expected a Class, ParameterizedType, or GenericArrayType, but <?> is of type com.google.inject.internal.MoreTypes$WildcardTypeImpl
绑定bind(new TypeLiteral<ListEditorFactory<NoteDTO, CharacterNoteScreen>>() {}).to(new TypeLiteral<ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen>>() {});
正在工作,但通用工厂失去了很多兴趣。
您知道声明此绑定的通用方法吗?
谢谢