Gwt 如何通过GIN与UiBinder和Widget一起使用injection?

Gwt 如何通过GIN与UiBinder和Widget一起使用injection?,gwt,dependency-injection,uibinder,gwt-gin,Gwt,Dependency Injection,Uibinder,Gwt Gin,我将GWT2.4与GWT平台0.7和gin 1.5.0一起使用 我已经为GWT应用程序的动态(实时)翻译构建了一个库。因此,当触发LocaleChangeEvent时,每个小部件都会收到通知,然后要求我的TranslationDictionary获取要显示的新字符串 小部件实际上如下所示: public class LocaleAwareLabel extends Label implements LocaleChangeEventHandler { TranslationDiction

我将GWT2.4与GWT平台0.7和gin 1.5.0一起使用

我已经为GWT应用程序的动态(实时)翻译构建了一个库。因此,当触发
LocaleChangeEvent
时,每个小部件都会收到通知,然后要求我的
TranslationDictionary
获取要显示的新字符串

小部件实际上如下所示:

public class LocaleAwareLabel extends Label implements LocaleChangeEventHandler {
    TranslationDictionary dictionary;
    String translationToken;

    public LocaleAwareLabel(TranslationDictionary dictionary, EventBus eventBus, String translationToken) {
        this.dictionary = dictionary;
        this.translationToken = translationToken;
        eventBus.addHandler(LocaleChangeEvent.TYPE, this);
        getCurrentTranslationFromDictionary();
    }

    public void getCurrentTranslationFromDictionary() {
        this.setText(dictionary.getTranslation(translationToken));
    }

    @Override
    public void onLocaleChange(LocaleChangeEvent event) {
        getCurrentTranslationFromDictionary();
    }
}
@UiField(provided=true)
LocaleAwareLabel myLabel;

@Inject
public MyView(TranslationDictionary dictionary, EventBus eventBus) {
    widget = uiBinder.createAndBindUi(this);

    myLabel = new LocaleAwareLabel(dictionary, eventBus, "someTranslationToken");
}
<custom:LocaleAwareLabel ui:field="myLabel" translationToken="someTranslationToken" />
如您所见:我无法轻松地将此小部件与UiBinder一起使用,目前我在我的
视图中插入
EventBus
TranslationDictionary
,并使用
@UiField(提供=true)
如下:

public class LocaleAwareLabel extends Label implements LocaleChangeEventHandler {
    TranslationDictionary dictionary;
    String translationToken;

    public LocaleAwareLabel(TranslationDictionary dictionary, EventBus eventBus, String translationToken) {
        this.dictionary = dictionary;
        this.translationToken = translationToken;
        eventBus.addHandler(LocaleChangeEvent.TYPE, this);
        getCurrentTranslationFromDictionary();
    }

    public void getCurrentTranslationFromDictionary() {
        this.setText(dictionary.getTranslation(translationToken));
    }

    @Override
    public void onLocaleChange(LocaleChangeEvent event) {
        getCurrentTranslationFromDictionary();
    }
}
@UiField(provided=true)
LocaleAwareLabel myLabel;

@Inject
public MyView(TranslationDictionary dictionary, EventBus eventBus) {
    widget = uiBinder.createAndBindUi(this);

    myLabel = new LocaleAwareLabel(dictionary, eventBus, "someTranslationToken");
}
<custom:LocaleAwareLabel ui:field="myLabel" translationToken="someTranslationToken" />
我想要的:在不使用
@UiField(provided=true)
的情况下使用我的小部件,因此我可以简单地将它们放入
ui.xml中,如下所示:

public class LocaleAwareLabel extends Label implements LocaleChangeEventHandler {
    TranslationDictionary dictionary;
    String translationToken;

    public LocaleAwareLabel(TranslationDictionary dictionary, EventBus eventBus, String translationToken) {
        this.dictionary = dictionary;
        this.translationToken = translationToken;
        eventBus.addHandler(LocaleChangeEvent.TYPE, this);
        getCurrentTranslationFromDictionary();
    }

    public void getCurrentTranslationFromDictionary() {
        this.setText(dictionary.getTranslation(translationToken));
    }

    @Override
    public void onLocaleChange(LocaleChangeEvent event) {
        getCurrentTranslationFromDictionary();
    }
}
@UiField(provided=true)
LocaleAwareLabel myLabel;

@Inject
public MyView(TranslationDictionary dictionary, EventBus eventBus) {
    widget = uiBinder.createAndBindUi(this);

    myLabel = new LocaleAwareLabel(dictionary, eventBus, "someTranslationToken");
}
<custom:LocaleAwareLabel ui:field="myLabel" translationToken="someTranslationToken" />
但是,由于
EventBus
TranslationDictionary
的原因,我仍然存在无法使用零参数构造函数的问题。另外,我不能在构造函数中调用
getCurrentTranslationFromDictionary()
,因为
translationToken
的值当然是在构造函数之后设置的

如果有人能提供一个解决方案,也许有代码示例,那就太好了

另外,我是个彻头彻尾的傻瓜,但据我所知,杜松子酒也许能解决我的问题。但我不知道怎么做


谢谢大家!

这在当前是不可能的,因为UiBinder永远不会调用GIN来实例化小部件。为此,您必须使用
@UiFactory
方法,或者使用
@Uifield(provided=true)
提供已构建的实例

为了更好地整合两者,已经有了增强的要求。您可以在此处跟踪它:

作为替代方案,您可以
requestStaticInjection
提供程序
注入
静态
字段,并从构造函数中调用提供程序的
get()

public类LocaleAwareLabel扩展标签{
@注入提供者eventBusProvider;
@注入提供者词典提供者;
私人最终翻译词典;
专用最终事件总线事件总线;
私有最终字符串translationToken;
@UIC构造函数
公共LocaleAwareLabel(字符串translationToken){
这(dictionaryProvider.get()、eventBusProvider.get()、translationToken);
}
//适用于您可以使用注入或自己注入参数的情况
@注入
公共LocaleAwarelabel(TranslationDictionary字典、EventBus EventBus、,
字符串translationToken){
这本字典=字典;
this.eventBus=eventBus;
this.translationToken=translationToken;
}

您可能还对GIN对辅助注入的支持感兴趣,以使编写
@UiFactory
方法或初始化
@UiField(provided=true)
字段更容易。

依赖注入和UiBinder之间目前有一点概念不匹配,但我目前使用它的方式是:

私人最终提供商标签提供商;
@注入
公共MyView(翻译词典),
事件总线事件总线,
提供者标签(提供者){
这本字典=字典;
this.labelProvider=labelProvider;
initWidget(uiBinder.createAndBindUi(this));
}
@UIF工厂
公共localeawarelable buildLocaleAwareLabel(){
返回labelProvider.get();
}
然后,我可以在ui.xml中创建任意数量的标签:


在LocaleAwareLabel中,我对翻译标记使用setter方法,并重写
onLoad()

私有字符串translationToken;
@注入
公共LocaleAwareLabel(TranslationDictionary字典,EventBus EventBus){
这本字典=字典;
initWidget(uiBinder.createAndBindUi(this));
}
@凌驾
受保护的void onLoad(){
super.onLoad();
doSomethingWithTranslationToken();//在此处执行此操作,而不是在构造函数中执行此操作!
}
public void setTranslationToken(最终字符串translationToken){
this.translationToken=translationToken;
}
AssistedInject示例 MyView更改为:

私有最终本地eAwareLabelFactory labelFactory;
@注入
公共MyView(翻译词典),
事件总线事件总线,
LocaleAwareLabelFactory(实验室工厂){
这本字典=字典;
this.labelFactory=labelFactory;
initWidget(uiBinder.createAndBindUi(this));
}
@UIF工厂
公共LocaleAwareLabel buildLocaleAwareLabel(最终字符串translationToken){
返回labelFactory.create(translationToken);
}
LocaleAwareLabel:

公共接口LocaleAwareLabelFactory{
LocaleAwareLabel创建(最终字符串translationToken);
}
@注入
公共LocaleAwareLabel(TranslationDictionary,
事件总线事件总线,
@辅助字符串translationToken){
这本字典=字典;
initWidget(uiBinder.createAndBindUi(this));
DoSomethingWithTranslationToken();//现在您可以在构造函数中执行此操作了!
}
在您的Gin模块中,添加:

install(新的GinFactoryModuleBuilder().build(LocaleAwareLabelFactory.class));

请确保将guice的assistedinject jar(例如guice assistedinject snapshot.jar)添加到您的类路径中。

谢谢。
@UiFactory
看起来与
@UiField(provided=true)
非常相似。
@UiConstructor
方法看起来更像我想要的,但仍然不完美!在我看来还有两种可能性(但我不知道如何实现它们):
1。
我在某个地方读到了GinUiBinder(来自gwt平台)。
2。
从我读到的内容来看,我认为gin可以提供某种工厂,自动注入
EventBus
TranslationDictionary
,然后只提供
LocaleAwareLabel(String translationToken)
constructor。或者这不可能是因为
@uiConstructu吗