如何在PlayN中使用Gin模块的Guice绑定?

4
假设你有一个使用Guice绑定的PlayN游戏核心项目:
/* PlayN core Guice module */
public class MyGameModule extends AbstractModule {

    @Override
    protected void configure() {
        bindConstant().annotatedWith(Title.class).to("My game title");
        // Other bindings...
    }

}

如果您想在使用Gin的GWT项目中使用相同的绑定,该怎么办呢?
对于Java项目来说,这是最简单的事情:
/* PlayN Java Guice module */
public class MyGameModuleJava extends MyGameModule {

    // **OK**: no manual intervention required.

}

这是否可以实现,而无需手动将Guice模块的configure方法复制粘贴到Gin模块中?例如,以下方法可作为解决方法,但不是我首选的方法:

/* PlayN GWT Gin module */
public class MyGameModuleHtml extends AbstractGinModule {

    @Override
    protected void configure() {
        // **NOK**: copy-paste the Guice module, as shown below.
        bindConstant().annotatedWith(Title.class).to("My game title");
        // Other bindings...
    }

}

它能工作,但说实话并不美观。

底线: 我想在所有PlayN项目中使用相同的绑定

解决方案

因此,我在我的Java主文件中使用了GinModuleAdapter,基于这个答案:

Injector injector = Guice.createInjector(new GinModuleAdapter(new MyGameModuleHtml()));

这样我也可以在核心类中删除Guice绑定。
1个回答

2
你应该尝试反过来做。例如,在GWT项目中定义绑定,然后通过GinModuleAdapter在Guice中使用(它允许你从Gin模块创建Guice模块)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接