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 862763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:11:52+00:00 2026-05-15T09:11:52+00:00

Here’s a simplified version of my needs. I have a program where every B

  • 0

Here’s a simplified version of my needs.

I have a program where every B object has its own C and D object, injected through Guice. In addition an A object is injected into every C and D objects.

What I want: that for each B object, its C and D objects will be injected with the same A object.

[Edit-Start]

(1) Guice supports “singleton” and “prototype” modes. However, what I need is something in between: I need A to be singleton WRT to a given B object (so that the C and D injected into a B object will share an A object). For another B object, I want another A. So it’s a singleton but for a limited scope of the program (actually, a limited scope of a data structure).

(2) I don’t mind a solution that uses method (setter)- or field- injection.

(3) I tried, several times, to achieve this and it always felt that I only need to implement some custom thingy of the DI container to make this work – but it never worked. Thus, I am looking for a detailed solution (not just “hand waving”)

[Edit-End]

Specifically, I want the output of the program (below) to be:

Created C0 with [A0]
Created D0 with [A0]
Created B0 with [C0, D0]
Created C1 with [A1]
Created D1 with [A1]
Created B1 with [C1, D1]

Where it currently produces the following output:

Created C0 with [A0]
Created D0 with [A1]  <-- Should be A0
Created B0 with [C0, D0]
Created C1 with [A2]  <-- Should be A1
Created D1 with [A3]  <-- Should be A1
Created B1 with [C1, D1]

I am expecting DI containers to allow this kind of customization but so far I had no luck in finding a solution. Below is my Guice-based code, but a Spring-based (or other DI containers-based) solution is welcome.

  import java.util.Arrays;
  import com.google.inject.*;

  public class Main {

    public static class Super {
      private static Map<Class<?>,Integer> map = new HashMap<Class<?>,Integer>();

      private Integer value;

      public Super(Object... args) {
        value = map.get(getClass());
        value = value == null ? 0 : ++value;
        map.put(getClass(), value);

        if(args.length > 0)
          System.out.println("Created " + this + " with " + Arrays.toString(args));
      }

      @Override
      public final String toString() {
        return "" + getClass().getSimpleName().charAt(0) + value;
      }
    }

    public interface A { }  
    public static class AImpl extends Super implements A  { } 

    public interface B { }  
    public static class BImpl extends Super implements B {
      @Inject public BImpl(C c, D d) { super(c,d); }
    }

    public interface C { }  
    public static class CImpl extends Super implements C  {
      @Inject public CImpl(A a) { super(a); }
    }

    public interface D { }  
    public static class DImpl extends Super implements D {
      @Inject public DImpl(A a) { super(a); }
    }


    public static class MyModule extends AbstractModule {
      @Override
      protected void configure() {
        bind(A.class).to(AImpl.class);
        bind(B.class).to(BImpl.class);      
        bind(C.class).to(CImpl.class);      
        bind(D.class).to(DImpl.class);      
      }    
    }

    public static void main(String[] args) {
      Injector inj = Guice.createInjector(new MyModule());
      inj.getInstance(B.class);    
      inj.getInstance(B.class);    
    }  
  }
  • 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-05-15T09:11:52+00:00Added an answer on May 15, 2026 at 9:11 am

    Here’s one solution based on your original code – there are three changes:

    1. Move the bindings for A, C, and D into a separate sub-module
    2. Mark A as a singleton in the sub-module
    3. Use a @Provides method in the main-module to provide instances of BImpl with
      a new child injector for each request – this is where the sub-module comes in

    This works because the singleton binding for A is now limited to each child injector.

    [ Note: you could always cache the sub-module instance in a field of the
    main-module if you don’t want to keep creating it for each request of B ]

      import java.util.*;
      import com.google.inject.*;
    
      public class Main {
    
        public static class Super {
          private static Map<Class<?>,Integer> map = new HashMap<Class<?>,Integer>();
    
          private Integer value;
    
          public Super(Object... args) {
            value = map.get(getClass());
            value = value == null ? 0 : ++value;
            map.put(getClass(), value);
    
            if(args.length > 0)
              System.out.println("Created " + this + " with " + Arrays.toString(args));
          }
    
          @Override
          public final String toString() {
            return "" + getClass().getSimpleName().charAt(0) + value;
          }
        }
    
        public interface A { }  
        public static class AImpl extends Super implements A  { } 
    
        public interface B { }  
        public static class BImpl extends Super implements B {
          @Inject public BImpl(C c, D d) { super(c,d); }
        }
    
        public interface C { }  
        public static class CImpl extends Super implements C  {
          @Inject public CImpl(A a) { super(a); }
        }
    
        public interface D { }  
        public static class DImpl extends Super implements D {
          @Inject public DImpl(A a) { super(a); }
        }
    
        public static class MyModule extends AbstractModule {
          @Override
          protected void configure() {} 
    
      // >>>>>>>>
          @Provides
          B builder( Injector injector ) {
            return injector.createChildInjector( new SubModule() ).getInstance( BImpl.class );
          }
      // <<<<<<<<
        }
    
      // >>>>>>>>
        public static class SubModule extends AbstractModule {
          @Override
          protected void configure() {
            bind(A.class).to(AImpl.class).in( Scopes.SINGLETON );
            bind(C.class).to(CImpl.class);      
            bind(D.class).to(DImpl.class);      
          }    
        }
      // <<<<<<<<
    
        public static void main(String[] args) {
          Injector inj = Guice.createInjector(new MyModule());
          inj.getInstance(B.class);    
          inj.getInstance(B.class);    
        }  
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.