3

我有以下 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.
    }
}

我想FizzModule用一个Buzz实例“注入”。但是,我看到的所有 GWT 模块代码示例都没有使用构造函数。相反,它们从方法内部引导 DI 机制(通常是 ClientFactory 或 GIN)onModuleLoad()。这是 GWT 强制执行的,还是我可以在模块加载到客户端之前以某种方式注入模块?提前致谢!

4

2 回答 2

2

GWT 总是使用它的零参数构造函数来实例化你的模块。

(从技术上讲,我认为它使用GWT.create(),所以你可以使用延迟绑定规则,但这不会改变任何东西。它是如何实例化的)

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

于 2012-11-15T16:14:30.663 回答
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();
        }
    }
}
于 2012-11-16T00:18:38.237 回答