Java 我可以将参数传递给GWT模块构造函数吗?
我有以下GWT模块:Java 我可以将参数传递给GWT模块构造函数吗?,java,gwt,dependency-injection,gwt-gin,Java,Gwt,Dependency Injection,Gwt Gin,我有以下GWT模块: public class FizzModule implements EntryPoint { private Buzz buzz; public FizzModule() { this(null); } public FizzModule(Buzz bz) { super(); setBuzz(bz); } @Override public void onMod
public class FizzModule implements EntryPoint {
private Buzz buzz;
public FizzModule() {
this(null);
}
public FizzModule(Buzz bz) {
super();
setBuzz(bz);
}
@Override
public void onModuleLoad() {
// ...etc.
}
}
我想用
Buzz
实例“注入”FizzModule
。然而,我看到的所有GWT模块的代码示例都没有使用构造函数。相反,它们从onModuleLoad()
方法内部引导DI机制(通常是ClientFactory或GIN)。这是GWT强制的吗?或者我可以在模块加载到客户端之前以某种方式注入模块吗?提前谢谢 GWT总是使用其零参数构造函数实例化模块
(从技术上讲,我认为它使用了GWT.create()
,因此您可以使用延迟绑定规则,但这不会改变它的实例化方式)
顺便说一句,
Buzz
实例从何而来?您可以向URL添加参数并使用PlaceController。然后在模块加载时获取这些值
public void onModuleLoad() {
SimplePanel mainPanel = new SimplePanel();
EventBus eventBus = GWT.creat(EventBus.class);
// Start ActivityManager for the main widget with ActivityMapper
ActivityManager activityManager = new ActivityManager(injector.getActivityMapper(),
eventBus);
activityManager.setDisplay(mainPanel);
RootPanel.get().add(mainPanel);
// Start PlaceHistoryHandler with our PlaceHistoryMapper
AppPlaceHistoryMapper contentHistoryMapper = GWT.create(AppPlaceHistoryMapper.class);
PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(contentHistoryMapper);
PlaceController placeController = new PlaceController(eventBus)
historyHandler.register(placeController, injector.getEventBus(), new MainPlace());
// Goes to the place represented on URL else default place
historyHandler.handleCurrentHistory();
if(placeController.getWhere() instanceof MainPlace) {
(MainPlace).getFoo();
}
}
public class MainPlace extends Place {
private String foo;
public MainPlace(String token) {
String foo = token;
}
@Override
public String getFoo() {
return foo;
}
public static class Tokenizer implements PlaceTokenizer<MainPlace> {
@Override
public MainPlace getPlace(String token) {
return new MainPlace(token);
}
@Override
public String getToken(MainPlace place) {
return place.getFoo();
}
}
}
模块加载()上的公共void{
SimplePanel mainPanel=新SimplePanel();
EventBus-EventBus=GWT.create(EventBus.class);
//使用ActivityMapper为主小部件启动ActivityManager
ActivityManager ActivityManager=新的ActivityManager(injector.getActivityMapper(),
事件总线);
activityManager.setDisplay(主面板);
RootPanel.get().add(主面板);
//使用我们的PlaceHistoryMapper启动PlaceHistoryHandler
AppPlaceHistoryMapper contentHistoryMapper=GWT.create(AppPlaceHistoryMapper.class);
PlaceHistoryHandler historyHandler=新的PlaceHistoryHandler(contentHistoryMapper);
PlaceController PlaceController=新的PlaceController(eventBus)
寄存器(placeController,injector.getEventBus(),newmainplace());
//转到URL上表示的位置或默认位置
historyHandler.HandleCurrenthHistory();
if(placeController.getWhere()instanceof MainPlace){
(MainPlace.getFoo();
}
}
公共类主要场所扩展场所{
私人字符串foo;
公共MainPlace(字符串令牌){
字符串foo=token;
}
@凌驾
公共字符串getFoo(){
返回foo;
}
公共静态类标记器实现PlaceTokenizer{
@凌驾
公共MainPlace getPlace(字符串令牌){
返回新的主位置(令牌);
}
@凌驾
公共字符串getToken(MainPlace){
return place.getFoo();
}
}
}
我不知道这就是为什么我说我是新来的