2

我编写了一个能够创建任何类型的 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>>() {});正在工作,但通用工厂失去了很多兴趣。

您知道声明此绑定的通用方法吗?

谢谢

4

1 回答 1

1

我设法通过摆脱ListEditorFactory接口找到了解决方案,不再需要在 gin 模块中声明绑定。

我可以直接注入实现没有任何问题:

@Inject
protected void init(ListEditorFactoryImpl<NoteDTO, CharacterNoteScreen> listEditorFactory) {
    notesEditor = listEditorFactory.create(notePanel);

如果 gin 能够注入它,我看不出它不能通过接口注入它但绑定声明错误的原因。

因此,即使我对我的解决方案感到满意,我也会提出问题,因为这可能是一种声明绑定的方式。

于 2013-07-25T22:59:39.297 回答