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.
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.
Now you can request a
@Named("user1") Userto 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 toKey.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.)
Now your User will be available by injecting
UserFactoryand callinguserFactory.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!