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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:07:21+00:00 2026-06-15T19:07:21+00:00

I have the following situation : public class User { @Inject private MusicInterface movie;

  • 0

I have the following situation :

public class User {

@Inject
private MusicInterface movie;

}

public interface MusicInterface {
public void getTitle();
}

and have Guice configuration class:

public class Module extends AbstractModule {

    @Override
    protected void configure() {

        bind(MusicInterface.class).annotatedWith(Names.named("rock")).to(RockMusic.class);
        bind(MusicInterface.class).annotatedWith(Names.named("pop")).to(PopMusic.class);
        bind(User.class).annotatedWith(Names.named("user1").to(User.class);
        bind(User.class).annotatedWith(Names.named("user2").to(User.class);
    }
}

My question would be:
how to inject into user class my wanted music bind?

For example:

in my main i want to get User1 with injected class named rock:

Injector injector = Guice.createInjector(new Module());

User user1 = injector.getInstance(Key.get(User.class,Names.named("user1")));

lets say user1 has attribute RockMusic.class, later on i want to get user2 with pop music:

User user2 = injector.getInstance(Key.get(User.class,Names.named("user2")));

And this class would have attribute PopMusic.class.

How can i do this?

I know that i can use annotation @Named(…) in the User class, but its not a solution in my case.

  • 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-15T19:07:22+00:00Added an answer on June 15, 2026 at 7:07 pm

    It sounds like what you’re trying to do is to bind different varieties of Music based on the annotation on the User. This is also called the Robot Legs problem, except you can replace “Leg” with “User” and “Foot” with “Music”. This is possible using Private Modules in Guice, but you may find it easier to create a UserFactory instead. I’ll show you both ways.

    First, the Private Module way. Private modules hide all of their bindings from the outside world, so you can expose certain ones without having the others conflict.

    public class MyModule extends AbstractModule {
      @Override protected void configure() {
        install(privateModuleFor("user1", RockMusic.class));
        install(privateModuleFor("user2", PopMusic.class));
      }
    
      private static Module privateModuleFor(
          final String userName, final Class<? extends MusicInterface> musicClass) {
        return new PrivateModule() {
          @Override protected void configure() {
            // Bind the annotated user.
            bind(User.class).annotatedWith(Names.named(userName)).to(User.class);
            // Now bind the music class as if it's the only music class ever.
            bind(MusicInterface.class).to(musicClass);
            // The above two bindings are hidden, and available only in this module,
            // so Guice doesn't complain about two different competing
            // MusicInterfaces. In order to get access to the User binding
            // outside the module, expose it.
            expose(User.class).annotatedWith(Names.named(userName));
          }
        };
      }
    }
    

    Now you can request a @Named("user1") User to get a User that likes rock music. Of course, you don’t have to organize it like I did with the method: You can still bind the types of MusicInterface to names if you’d like, and then bind the annotation-less MusicInterface to Key.get(MusicInterface.class, yourMusicTypeHere) in your PrivateModule, but unless you need the named MusicInterfaces elsewhere you can probably skip that step.

    The other way is to skip using Guice for this binding. Guice is a great solution, but for some complicated binding situations it can be more trouble than it’s worth to set up. Your alternative is to create a lightweight UserFactory that picks the right type of Music for you. (ImmutableMap is from Guava.)

    public class UserFactory {
      private Map<String, Class<? extends MusicInterface>> musicMap =
          ImmutableMap.builder()
            .put("user1", RockMusic.class)    // You could also put instances here
            .put("user2", PopMusic.class)     // and skip using Guice entirely!
            .build();
    
      // Never inject Injector unless you don't know what you need until runtime,
      // which is exactly what is happening here.
      @Inject private Injector injector;
      @Inject private Provider<Dependency> someOtherUserDependency;
    
      public User createUser(String musicType) {
        Class<? extends MusicInterface> clazz = musicMap.get(musicType);
        return new User(injector.getInstance(clazz), someOtherUserDependency.get());
      }
    }
    

    Now your User will be available by injecting UserFactory and calling userFactory.create(). Of course, maybe your User needs a dozen other injected dependencies. In that case, the first option looks good, or you may investigate assisted injection. All of these are workable options, so you’ll need to think about which one is the right combination of flexibility and readability for your situation.

    Hope that helps!

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

Sidebar

Related Questions

Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have the following situation : Class ServiceCaller: public class ServiceCaller{ private Service service;
I have following situation class base { public: virtual void Accepted(SOCKET s) //// Event
I have the following situation: public abstract class A { private Object superMember; public
I have the following situation: I have class User with the follwing properies :
I have the following situation: // A public interface of some kind public interface
Imagine I have the following situation: Test1.java import java.lang.ref.WeakReference; public class Test1 { public
I have a problem with JPA 1.0 (OpenJPA) Following situation @Entity public class A{
I have following class: public class PairOfDice { private Dice d1,d2; public int Value
I have the following helper class (simplified): public static class Cache { private static

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.