gpt4 book ai didi

java - 使用 Multibinding 概括 guice 的机器人腿示例

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:03:49 26 4
gpt4 key购买 nike

我有一个与 Guice 的机器人腿示例非常相似的用例,只是我不知道我有多少条“腿”。因此我不能使用机器人腿示例所需的注释。

我希望使用 Guice 的 Multibindings 扩展将所有这些“腿”收集到一个 java.util.Set 中。

从技术上讲,在 PrivateModule 中,我想将一个实现直接公开为将由 Multibindings 扩展提供的集合的一个元素。我只是不知道该怎么做。

有关引用和代码示例,请参见此处的机器人腿示例:http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions#How_do_I_build_two_similar_but_slightly_different_trees_of_objec


这是我的精确用例:

我有以下内容:

// Main application
public interface MyTree {...}
public interface MyInterface {
public MyTree getMyTree() {}
}
public abstract class MyModule extends PrivateModule {}
public class MyManager {
@Inject MyManager (Set<MyInterface> interfaces){ this.interfaces = interfaces }
}
public class MainModule extends AbstractModule {
public void configure () {
// Install all MyModules using java.util.ServiceLoader.
}
}


// In expansion "square.jar"
public class SquareTree implements MyTree {...}
public class SquareImplementation implements MyInterface {
@Inject SquareImplementation (MyTree tree) { this.tree = tree; }
public MyTree getMyTree () { return this.tree; }
}
public class SquareModule extends MyModule { // correctly defined as a ServiceLoader's service.
public void configure () {
// How to make this public IN a multibinder's set?
bind(MyInterface.class).to(SquareImplementation.class);

// Implementation specific to the Squareimplementation.
bind(MyTree.class).to(SquareTree.class);
}
}

// In expansion "circle.jar"
public class CircleTree implements MyTree {...}
public class CircleImplementation implements MyInterface {
@Inject CircleImplementation (MyTree tree) { this.tree = tree; }
public MyTree getMyTree () { return this.tree; }
}
public class CircleModule extends MyModule { // correctly defined as a ServiceLoader's service.
public void configure () {
// How to make this public IN a multibinder's set?
bind(MyInterface.class).to(CircleImplementation.class);

// Implementation specific to the Circle implementation.
bind(MyTree.class).to(CircleTree.class);
}
}

因为我说的是扩展 jar ,一开始我不知道它们,我什至不知道它们有多少:我需要用 MyModule 加载 j.u.ServiceLoader 并且每个模块都应该定义一个MyInterface实现(这两部分都可以)。

问题是将所有 MyInterface 实现集中在一个集合中(在 MyManager 中)。我该怎么做?


解决方案,完全基于 Jesse 的回答:

// Create the set binder.
Multibinder<MyInterface> interfaceBinder = Multibinder.newSetBinder(binder(), MyInterface.class, MyBinding.class);

// Load each module that is defined as a service.
for (final MyModule module : ServiceLoader.load(MyModule.class)) {

// Generate a key with a unique random name, so it doesn't interfere with other bindings.
final Key<MyInterface> myKey = Key.get(MyInterface.class, Names.named(UUID.randomUUID().toString()));
install(new PrivateModule() {
@Override protected void configure() {
// Install the module as part of a PrivateModule so they have full hands on their own implementation.
install(module);
// Bind the unique named key to the binding of MyInterface.
bind(myKey).to(MyInterface.class);
// Expose the unique named binding
expose(myKey);
}
});
// bind the unique named binding to the set
interfaceBinder.addBinding().to(myKey);
}

如果 MyModule 是扩展 Module 的接口(interface),这允许我不强制“客户”扩展 PrivateModule,而是使用任何模块实现。

最佳答案

看起来您需要跳过一些环节才能从私有(private)模块中 promise 绑定(bind),以便它们可以位于顶级注入(inject)器的多重绑定(bind)中。

这应该有效:

public class SquareModule extends AbstractModule { // does not extend PrivateModule
@Overide public void configure() {
// this key is unique; each module needs its own!
final Key<MyInterface> keyToExpose
= Key.get(MyInterface.class, Names.named("square"));

install(new PrivateModule() {
@Override public void configure() {

// Your private bindings go here, including the binding for MyInterface.
// You can install other modules here as well!
...

// expose the MyInterface binding with the unique key
bind(keyToExpose).to(MyInterface.class);
expose(keyToExpose);
}
});

// add the exposed unique key to the multibinding
Multibinder.newSetBinder(binder(), MyInterface.class).addBinding().to(keyToExpose);
}
}

此解决方法是必要的,因为多重绑定(bind)需要发生在顶级注入(inject)器中。但是私有(private)模块绑定(bind)对该注入(inject)器不可见,因此您需要公开它们。

关于java - 使用 Multibinding 概括 guice 的机器人腿示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6625837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com