0
我经常需要一个客户端软件包和一些国际化的演示文稿和视图中的消息。GWT注射:需要良好的练习
我想知道哪些是最好的方法让他们:注射或Singleton?
解决方案1:到现在为止,我用使用的消息辛格尔顿得到:
public interface MyMessages extends Messages{
String key1();
String key2();
...
class Instance {
private static MyMessages instance = null;
public static MyMessages getInstance() {
if (instance == null) {
instance = GWT.create(MyMessages.class);
}
return instance;
}
}
}
FooView.java:
MyMessages.Instance.getInstance().key1();
解决方案2:它会更好用这样的注射来得到它?
private MyMessages i18n;
@Inject
public FooView(MyMessages i18n){
this.i18n=i18n;
}
第二个解决方案似乎更清洁给我,但我有时会卡住,当我需要它使用了一些国际化的字符串非空的构造:
@Inject
private MyMessages i18n;
public Bar(Foo foo){
/*
* do something which absolutely requires i18n here.
* The problem is that injectable attributes are called
* after the constructor so i18n is null here.
*/
foobar();
}
“非空构造函数”=>“非空构造函数“,对不起:) – user2147970 2013-04-11 13:05:42
然后注入'MyMessages'作为构造函数参数有什么问题? (就像你在上面的'FooView'中做的那样) – 2013-04-11 13:18:25
我其实并不知道邻接辅助注入,因此我阅读了关于它的google文档。谢谢你,这在一些使用情况下非常有用。也就是说,对于需要国际化或捆绑的每个班级使用辅助注射似乎很重要。因此,对于这个用例,我将继续使用GWT.create():) – user2147970 2013-04-15 06:54:08