Guice Binder具有绑定功能

时间:2016-05-07 18:15:55

标签: java guice

有没有办法检查接口是否存在实现?

例如,下面的方法“hasBind”不存在:

public class MyModule extends AbstractModule {
    @Override
    protected void configure() {
        // do something like this:
        if (!hasBind(SomeInterface.class)) {
            bind(SomeInterface.class).to(MyOtherSomeInterface.class);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于应用模块的顺序在guice中无关紧要,因此在创建注入器之前无法检测绑定是否存在,请参阅this post

建议的解决方案是使用默认绑定创建一个启动模块,并使用Modules.overwrite来应用ypur其他绑定。