【问题标题】:Can I pass arguments into GWT Module constructor?我可以将参数传递给 GWT 模块构造函数吗?
【发布时间】:2012-11-15 15:47:31
【问题描述】:

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

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

【问题讨论】:

    标签: java gwt dependency-injection gwt-gin


    【解决方案1】:

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

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

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

    【讨论】:

    • 我不知道这就是为什么我说我是新人
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2011-04-04
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      相关资源
      最近更新 更多