Status: New
Owner: ----
New issue 548 by bestwing: Same Module Mutiple Implementation Problem
http://code.google.com/p/google-guice/issues/detail?id=548
public interface Player {
public void bat();
public void bowl();
}
=======================================================================
public class GoodPlayer implements Player {
public void bat() {
System.out.println("I can hit any ball");
}
public void bowl() {
System.out.println("I can also bowl");
}
}
=======================================================================
public class GoodPlayer implements Player {
public void bat() {
System.out.println("I can hit any ball");
}
public void bowl() {
System.out.println("I can also bowl");
}
}
=======================================================================
import java.lang.annotation.*;
import com.google.inject.BindingAnnotation;
@Retention(RetentionPolicy.RUNTIME)
@BindingAnnotation
@Target(ElementType.LOCAL_VARIABLE)
public @interface Good {}
=======================================================================
import java.lang.annotation.*;
import com.google.inject.BindingAnnotation;
@Retention(RetentionPolicy.RUNTIME)
@BindingAnnotation
@Target(ElementType.LOCAL_VARIABLE)
public @interface Bad {
}
=======================================================================
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Provides;
public class PlayerModule implements Module {
public void configure(Binder binder) {
binder.bind(Player.class).annotatedWith(Good.class).to(GoodPlayer.class);
binder.bind(Player.class).annotatedWith(Bad.class).to(BadPlayer.class);
}
@Provides
Player provideGoodPlayer() {
return new GoodPlayer();
}
@Provides
Player provideBadPlayer() {
return new BadPlayer();
}
}
=======================================================================
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
public class PlayerClient {
/**
* @param args
*/
public static void main(String[] args) {
PlayerModule module = new PlayerModule();
Injector injector = Guice.createInjector(new Module[] { module
});
@Good
Player player = (Player) injector.getInstance(Player.class);
player.bat();
player.bowl();
}
}
=======================================================================
I found the above sample program at
http://www.javabeat.net/articles/29-introduction-to-google-guice-5.html.
And it generates error when I try to run the PlayClient.
I found that I am unable to get the instance by the @Good annotation.
The error is:
Exception in thread "main" com.google.inject.CreationException: Guice
creation errors:
1) A binding to app.player.Player was already configured at
app.player.PlayerModule.provideGoodPlayer().
at app.player.PlayerModule.provideBadPlayer(PlayerModule.java:23)
1 error
at
com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:354)
at
com.google.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:152)
at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:105)
at com.google.inject.Guice.createInjector(Guice.java:92)
at com.google.inject.Guice.createInjector(Guice.java:69)
at com.google.inject.Guice.createInjector(Guice.java:59)
at app.player.PlayerClient.main(PlayerClient.java:14)
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.