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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:24:02+00:00 2026-06-02T01:24:02+00:00

Let’s make the statement All Rocks have Minerals. : public class Mineral { //

  • 0

Let’s make the statement “All Rocks have Minerals.“:

public class Mineral
{
    // Nevermind why a Mineral would have a GUID.
    // This is just to show that each Mineral instance
    // is universally-unique.
    String guid;

    @Inject
    public Mineral(String id)
    {
        guid = id;
    }
}

public class Rock
{
    private Mineral mineral;

    @Inject
    public Rock(Mineral min)
    {
        mineral = min;
    }
}

If we want 2 Rock instances, each of them configured with different Mineral instances (each with their own GUIDs):

public class RockModule extends AbstractModule
{
    public void configure(Binder binder)
    {
        // Make two Minerals with different GUIDs.
        Mineral M1 = new Mineral(UUID.getRandomId().toString());
        Mineral M2 = new Mineral(UUID.getRandomId().toString());

        // Configure two Rocks with these unique Minerals
        Rock R1 = new Rock(M1);
        Rock R2 = new Rock(M2);

        // Define bindings
        bind(Rock.class).toInstance(R1);

        // No way for Guice to expose R2 to the outside world!
    }
}

So now, when we ask Guice for a Rock, it will always give us the R1 instance, which itself is configured with the M1 instance of Mineral.

In Spring DI, you can define two beans to be the same type, but just give them different bean IDs. Then you “wire” beans together using their IDs. So I could wire R1 and M1 together, R1 and M2 together, etc. Then, I can ask Spring for R1 or R2 as I need them.

With Guice, you can only ask for the type you want (Rock.class), not the instance.

How do you request different “wired beans” with Guice? By using different AbstractModule concretions? Or is this a limitation of Guice?

  • 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-02T01:24:04+00:00Added an answer on June 2, 2026 at 1:24 am

    Generally this would be a violation of what Guice recommends. You would not typically new up a class inside of a module to create instances because it defeats the purpose of using a DI container in the first place. (Why then annotate your classes with @Inject?)

    Rather, you’d let Guice do this for you:

    class RockModule extends AbstractModule {
      public void configure() {}
    
      @Provides
      @Named("UUID")
      public String getUuid() {
        return UUID.getRandomId().toString();
      }
    }
    

    Now each Mineral automatically gets a unique UUID, and each Rock gets a unique Mineral.

    This gets us part way, but if you want two unique pairings of a Rock and Mineral for use at runtime, then this is what Guice calls the “robot legs” problem, and you can solve it using private modules. See an example here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's imagine I have a Java class of the type: public class MyClass {
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have one class User, and it has a property of type
Let's say you have a class library project that has any number of supplemental
Let's say I have an abstract parent class called shape, and that there are
Let me frame it this way.. Say I have an application server running 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.