के साथ गुइस के रोबोट-पैरों का उदाहरण सामान्य करें मेरे पास यह उपयोग केस है जो गुइस के रोबोट-पैर उदाहरण के समान है, सिवाय इसके कि मुझे नहीं पता कि मेरे पास कितने "पैर" हैं। इसलिए मैं रोबोट-पैरों के उदाहरण के लिए आवश्यक एनोटेशन का उपयोग नहीं कर सकता।मल्टीबाइंडिंग
मैं इन सभी "पैरों" को java.util.Set में गिस के मल्टीबिंडिंग एक्सटेंशन के साथ इकट्ठा करने की उम्मीद करता हूं।
तकनीकी रूप से, एक निजी मॉड्यूल में मैं एक कार्यान्वयन का पर्दाफाश करना चाहता हूं जो सेट के तत्व के रूप में सीधे मल्टीबिंडिंग एक्सटेंशन द्वारा प्रदान किया जाएगा। मुझे नहीं पता कि यह कैसे करना है।
संदर्भ और कोड उदाहरण के लिए, रोबोट-पैर यहाँ उदाहरण देखें:
मैं निम्नलिखित है:
// 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);
}
}
यहाँ मेरी सटीक उपयोग-मामला है मैं विस्तार जार के बारे में बात कर रहा हूं, मैं उन्हें पहले नहीं जानता, मुझे यह भी नहीं पता कि उनमें से कितने मौजूद हैं: मुझे एसके साथ लोड करने की आवश्यकता हैऔर प्रत्येक मॉड्यूल को MyInterface
कार्यान्वयन को परिभाषित करना चाहिए (ये दो भाग ठीक हैं)।
समस्या यह है कि एक सेट में सभी MyInterface
कार्यान्वयन (MyManager
में) प्राप्त करें। मैं उसे कैसे कर सकता हूँ?
// 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);
}
यह मैं एक PrivateModule विस्तार करने के लिए, बल्कि किसी भी मॉड्यूल कार्यान्वयन का उपयोग "ग्राहक" मजबूर नहीं करने के लिए करता है, तो MyModule एक अंतरफलक है कि फैली हुई है की अनुमति देता है:
समाधान, पूरी तरह से जेसी के जवाब पर आधारित मॉड्यूल।
धन्यवाद जेसी, हालांकि आपका जवाब पढ़ने के बाद, मुझे यकीन नहीं था कि मैंने समस्या को सही ढंग से समझाया है। तो मैंने एक उदाहरण के साथ अपने प्रश्न को सही किया। क्या यह अब और समझ में आता है? –
ठीक है, मैं आपके नए उत्तर का परीक्षण करूंगा और परिणाम यहां रिपोर्ट करूंगा। –
हां, यह पूरी तरह से काम करता है। इसे मॉड्यूलरिज़ेबल बनाने में काफी मुश्किल है, लेकिन यह ठीक है। –