1

我有以下情况:

public class User {

@Inject
private MusicInterface movie;

}

public interface MusicInterface {
public void getTitle();
}

并有 Guice 配置类:

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);
    }
}

我的问题是:如何将我想要的音乐绑定注入用户类?

例如:

在我的主要我想用名为rock的注入类获得User1:

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

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

假设 user1 有属性 RockMusic.class,稍后我想用流行音乐来获取 user2:

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

并且此类将具有属性 PopMusic.class。

我怎样才能做到这一点?

我知道我可以在 User 类中使用注释 @Named(...) ,但在我的情况下它不是解决方案。

4

2 回答 2

2

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!

于 2012-12-11T06:43:16.183 回答
0

鉴于@Named 不是一个选项,我认为您也不能使用绑定注释。您可以向注射器索要它:

Injector injector = Guice.createInjector(new Module());
MusicInterface musicInterface = injector.getInstance(Key.get(String.class, Names.named("rock"))
于 2012-12-10T19:46:09.320 回答