Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One workaround, that allows a <code>Multibinder</code> to be shared across private modules, is to wrap the <code>PrivateModule</code> in an <code>AbstractModule</code> implementation that simply installs the <code>PrivateModule</code> and binds the exposed key to the <code>Multibinder</code></p> <pre><code>import com.google.inject.AbstractModule; import com.google.inject.Exposed; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.PrivateModule; import com.google.inject.Provides; import com.google.inject.multibindings.Multibinder; import com.google.inject.name.Named; import com.google.inject.name.Names; import java.util.Set; public class GuiceCircularDependencyTest { public static void main(String[] args) { Injector in = Guice.createInjector(new Owner()); String result = in.getInstance(Key.get(String.class, Names.named("result"))); System.out.println("Result is: " + result); } public static class Owner extends PrivateModule { @Override protected void configure() { Multibinder&lt;String&gt; multi = Multibinder.newSetBinder(binder(), String.class); install(new Integration()); install(new ModuleWrapper&lt;&gt;(new ModuleA(), multi)); install(new ModuleWrapper&lt;&gt;(new ModuleB(), multi)); install(new ModuleWrapper&lt;&gt;(new ModuleC(), multi)); expose(String.class).annotatedWith(Names.named("result")); } } public static class ModuleWrapper&lt;T&gt; extends AbstractModule { private final WrappablePrivateModule&lt;T&gt; inner; private final Multibinder&lt;T&gt; multi; public ModuleWrapper(WrappablePrivateModule&lt;T&gt; inner, Multibinder&lt;T&gt; multi) { this.inner = inner; this.multi = multi; } @Override protected void configure() { install(inner); multi.addBinding().to(inner.getExposedKey()); } } public static abstract class WrappablePrivateModule&lt;T&gt; extends PrivateModule { @Override protected void configure() { } public abstract Key&lt;T&gt; getExposedKey(); } public static class ModuleA extends WrappablePrivateModule&lt;String&gt; { private static final String SUFFIX = "A"; @Override public Key&lt;String&gt; getExposedKey() { return Key.get(String.class, Names.named(SUFFIX)); } @Provides @Exposed @Named(SUFFIX) public String expose(Wrapper prefix) { return prefix + SUFFIX; } } public static class ModuleB extends WrappablePrivateModule&lt;String&gt; { private static final String SUFFIX = "B"; @Override public Key&lt;String&gt; getExposedKey() { return Key.get(String.class, Names.named(SUFFIX)); } @Provides @Exposed @Named(SUFFIX) public String expose(Wrapper prefix) { return prefix + SUFFIX; } } public static class ModuleC extends WrappablePrivateModule&lt;String&gt; { private static final String SUFFIX = "C"; @Override public Key&lt;String&gt; getExposedKey() { return Key.get(String.class, Names.named(SUFFIX)); } @Provides @Exposed @Named(SUFFIX) public String expose(Wrapper prefix) { return prefix + SUFFIX; } } public static class Integration extends PrivateModule { @Override protected void configure() { bind(Wrapper.class).toInstance(new Wrapper("Module")); expose(Wrapper.class); } @Provides @Exposed @Named("result") public String go(Set&lt;String&gt; in) { return in.toString(); } } public static class Wrapper { private final String prefix; public Wrapper(String prefix) { this.prefix = prefix; } @Override public String toString() { return prefix; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload