相关疑难解决方法(0)

Guice相当于Spring的@Autowire实例列表

春天的时候我这样做:

@Autowire
List<MyInterface> myInterfaces;
Run Code Online (Sandbox Code Playgroud)

然后这个列表将被所有实现的bean填充MyInterface.我没有创建类型的bean List<MyInterface>.

我在Google Guice中寻找此类行为.

Sofar我去了:

Multibinder<MyInterface> myInterfaceBinder = MultiBinder.newSetBinder(binder(), MyInterface.class);
Run Code Online (Sandbox Code Playgroud)

现在如果我有一个实现MyInterface并绑定它的bean ,请通过:

bind(MyInterfaceImpl.class).asEagerSingleton();
Run Code Online (Sandbox Code Playgroud)

它不会包含在我的multibinder中.我需要添加:

myInterfaceBinder.addBinding.to(MyInterfaceImpl.class);
Run Code Online (Sandbox Code Playgroud)

这比Spring提供的要复杂一些.所以我很惊讶我是不是以错误的方式使用它.那么有更简单的方法来实现这一目标吗?

java guice

7
推荐指数
1
解决办法
1778
查看次数

标签 统计

guice ×1

java ×1