Injecting a generic factory in Guice

后端 未结 2 987
梦如初夏
梦如初夏 2020-12-16 15:55

The following code is an example of a factory that produces a Bar given a Foo. The factory doesn\'t care what T is:

2条回答
  •  太阳男子
    2020-12-16 16:42

    One option is to just write the BarFactory boilerplate by hand:

    class BarImplFactory implements BarFactory {
      public  Bar create(Foo f) {
        return new BarImpl(f);
      }
    }
    

    The binding becomes

    bind(BarFactory.class).to(BarImplFactory.class);
    

提交回复
热议问题