Injecting Collection of Classes with Guice

后端 未结 2 927
醉酒成梦
醉酒成梦 2021-01-31 08:26

I\'m trying to inject things with Google Guice 2.0 and I have the following structure:

FooAction implements Action
BarAction implements Action

2条回答
  •  温柔的废话
    2021-01-31 09:07

    What you want for this is Multibindings. Specifically, you want to bind a Set (not a List, but a Set is probably what you really want anyway) like this:

    Multibinder actionBinder = Multibinder.newSetBinder(binder(), Action.class);
    actionBinder.addBinding().to(FooAction.class);
    actionBinder.addBinding().to(BarAction.class);
    

    Then you can @Inject the Set anywhere.

提交回复
热议问题