Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9104423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:51:54+00:00 2026-06-17T01:51:54+00:00

I am currently writing some framework code that provides a blueprint for services within

  • 0

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.

The blueprint has 2 types of logical component;

  1. 1 and only 1 integration component that integrates the service with the outside world (messaging middleware etc)
  2. 1-n business logic components

Each logic component depends on the integration component.

The integration component depends on all the logic components.

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).

My question is what approaches can I take to make this work without making service implementors write that boilerplate code?

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.

A contrived example to illustrate where

  1. business logic = ModuleA, ModuleB, ModuleC
  2. dependency provided by the integration = Wrapper
  3. Integration’s dependency on the business logic is modelled by each logic module adding something to the Wrapper

This example can be made to work by changing

@Provides @Exposed @Named("result")
public String go(Container in) {
    return in.format();
}

to

@Provides @Exposed @Named("result")
public String go(@Named("a") Container in, @Named("b") Container in2, @Named("c") Container in3) {
    return in.format();
}

i.e. by actually creating a circular dependency.

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<String> strings = new ArrayList<>();

        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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:51:55+00:00Added an answer on June 17, 2026 at 1:51 am

    One workaround, that allows a Multibinder to be shared across private modules, is to wrap the PrivateModule in an AbstractModule implementation that simply installs the PrivateModule and binds the exposed key to the Multibinder

    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<String> multi = Multibinder.newSetBinder(binder(), String.class);
                install(new Integration());
                install(new ModuleWrapper<>(new ModuleA(), multi));
                install(new ModuleWrapper<>(new ModuleB(), multi));
                install(new ModuleWrapper<>(new ModuleC(), multi));
                expose(String.class).annotatedWith(Names.named("result"));
            }
        }
    
        public static class ModuleWrapper<T> extends AbstractModule {
            private final WrappablePrivateModule<T> inner;
            private final Multibinder<T> multi;
    
            public ModuleWrapper(WrappablePrivateModule<T> inner,
                                 Multibinder<T> multi) {
                this.inner = inner;
                this.multi = multi;
            }
    
            @Override
            protected void configure() {
                install(inner);
                multi.addBinding().to(inner.getExposedKey());
            }
        }
    
        public static abstract class WrappablePrivateModule<T> extends PrivateModule {
    
            @Override
            protected void configure() {
    
            }
    
            public abstract Key<T> getExposedKey();
        }
    
        public static class ModuleA extends WrappablePrivateModule<String> {
    
            private static final String SUFFIX = "A";
    
            @Override
            public Key<String> 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<String> {
    
            private static final String SUFFIX = "B";
    
            @Override
            public Key<String> 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<String> {
    
            private static final String SUFFIX = "C";
    
            @Override
            public Key<String> 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<String> 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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some c# code that is currently supposed to run as fast
I'm currently writing several Python modules which perform some I/O. Thoses modules can be
I'm currently writing a java program that requires some Data to run. The data
I am currently writing a project that uses Django w/ Django-Rest-Framework on the backend
I'm currently writing a little django app to get some practice with the framework.
I'm currently writing some update polling stuff. I try to avoid writing even a
I am currently writing some extensive documentation using Sphinx for a rather complex Django
I'm currently writing some intranet web application where people could submit to admins requests
I'm currently writing some routines for the automation of pivot table generation. In order
I am currently writing a webapp in which some pages are heavily reliant on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.