Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle circular dependencies across multiple guice private modules?
    primarykey
    data
    text
    <p>I am currently writing some framework code that provides a blueprint for services within our platform so that service implementors can focus on the service specific logic rather than the boilerplate integration code. Dependency injection is done via guice. </p> <p>The blueprint has 2 types of logical component;</p> <ol> <li>1 and only 1 integration component that integrates the service with the outside world (messaging middleware etc) </li> <li>1-n business logic components </li> </ol> <p>Each logic component depends on the integration component. </p> <p>The integration component depends on <em>all</em> the logic components.</p> <p>Since this is framework code, the framework is not aware of any of the concrete details so it is not possible to statically declare the dependencies and form the dependency graph. I would like to avoid making service implementors do this because it means they are repeating themselves (just declaring that they have n business logic modules means they have this circular dependency).</p> <p>My question is what approaches can I take to make this work <strong>without</strong> making service implementors write that boilerplate code?</p> <p>Note that multibindings are not available here as, for various reasons outside the scope of this question, each business logic component must be a PrivateModule.</p> <p>A contrived example to illustrate where</p> <ol> <li>business logic = ModuleA, ModuleB, ModuleC</li> <li>dependency provided by the integration = Wrapper</li> <li>Integration's dependency on the business logic is modelled by each logic module adding something to the Wrapper</li> </ol> <p>This example can be made to work by changing</p> <pre><code>@Provides @Exposed @Named("result") public String go(Container in) { return in.format(); } </code></pre> <p>to</p> <pre><code>@Provides @Exposed @Named("result") public String go(@Named("a") Container in, @Named("b") Container in2, @Named("c") Container in3) { return in.format(); } </code></pre> <p>i.e. by actually creating a circular dependency. </p> <pre><code>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.Singleton; import com.google.inject.name.Named; import com.google.inject.name.Names; import java.util.ArrayList; import java.util.List; 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() { bind(Container.class).in(Singleton.class); install(new Integration()); install(new ModuleA()); install(new ModuleB()); install(new ModuleC()); expose(String.class).annotatedWith(Names.named("result")); } } public static class ModuleA extends PrivateModule { @Override protected void configure() { } @Provides @Exposed @Named("a") public Container go(Container in, Wrapper prefix) { in.add(prefix + "A"); return in; } } public static class ModuleB extends PrivateModule { @Override protected void configure() { } @Provides @Exposed @Named("b") public Container go(Container in, Wrapper prefix) { in.add(prefix + "B"); return in; } } public static class ModuleC extends PrivateModule { @Override protected void configure() { } @Provides @Exposed @Named("c") public Container go(Container in, Wrapper prefix) { in.add(prefix + "C"); return in; } } 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(Container in) { return in.format(); } } public static class Container { private List&lt;String&gt; strings = new ArrayList&lt;&gt;(); public void add(String string) { strings.add(string); } public String format() { return strings.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