我可以将参数传递给GWT Module构造函数吗?

时间:2012-11-15 15:47:31

标签: java gwt dependency-injection gin

我有以下GWT模块:

public class FizzModule implements EntryPoint {
    private Buzz buzz;

    public FizzModule() {
        this(null);
    }

    public FizzModule(Buzz bz) {
        super();

        setBuzz(bz);
    }

    @Override
    public void onModuleLoad() {
        // ...etc.
    }
}

我想"注射" FizzModuleBuzz个实例。但是,我在GWT模块中看到的所有代码示例都不使用构造函数。相反,他们从onModuleLoad()方法内部引导DI机制(通常是ClientFactory或GIN)。这是GWT强制要求的东西,还是我可以在加载到客户端之前以某种方式注入我的模块?提前谢谢!

2 个答案:

答案 0 :(得分:2)

GWT始终使用零参数构造函数实例化您的模块。

(从技术上讲,我认为它使用GWT.create()因此您可以使用延迟绑定规则,但这不会改变任何内容。如何实例化

顺便说一下,Buzz实例来自哪里?

答案 1 :(得分:0)

您可以向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();
        }
    }
}