Hi Stuart & Brandon, I tried the two solutions that you guys mentioned, both of them worked perfectly, thanks a lot.
Cheers, Jiakuan On 9月29日, 上午1时32分, Brandon Atkinson <[email protected]> wrote: > Sorry, incomplete example... > > You'd then have to call > > Game game = injector.getInstance(Game.class); > > On Mon, Sep 28, 2009 at 1:31 PM, Brandon Atkinson < > > [email protected]> wrote: > > Hi, > > > I think the problem is that your annotation is outside any scope that the > > injector can see. > > > You are calling: > > > @Good Player player = injector.getInstance(Player.class); > > > Because Player.class doesn't have an injection point annotated with @Good > > or @Bad, and you only put mappings for @Good and @Bad in your module, Guice > > will look for a no arg constructor for a concrete Player class, which you > > have not provided, because Player is an interface. > > > If you were to change your example to something like > > > class Game { > > private Player player; > > > @Inject public Game(@Good Player player) { > > player=this.player; > > } > > } > > > I don't think you would not see this error. > > > -Brandon > > > On Mon, Sep 28, 2009 at 5:00 AM, delightwjk <[email protected]> wrote: > > >> Hi All, > > >> I'm just starting to play with Guice - the beautiful lightweight DI > >> framework, I encountered a simple problem, didn't find answer over > >> internet, I guess someone here can help me, thanks very much in > >> advance. > > >> I have several Java files: > > >> ================================ > >> Player.java - The interface that I defined. > >> ================================ > >> package net.jiakuan.test.sample4; > > >> public interface Player { > >> public void bat(); > > >> public void bowl(); > >> } > > >> ================================ > >> GoodPlayer.java > >> ================================ > >> package net.jiakuan.test.sample4; > > >> 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"); > >> } > >> } > > >> ================================ > >> BadPlayer.java > >> ================================ > >> package net.jiakuan.test.sample4; > > >> public class BadPlayer implements Player { > >> public void bat() { > >> System.out.println("I think i can face the ball"); > >> } > > >> public void bowl() { > >> System.out.println("I dont know bowling"); > >> } > >> } > > >> ================================ > >> Good.java > >> ================================ > >> package net.jiakuan.test.sample4; > > >> import java.lang.annotation.ElementType; > >> import java.lang.annotation.Retention; > >> import java.lang.annotation.RetentionPolicy; > >> import java.lang.annotation.Target; > > >> import com.google.inject.BindingAnnotation; > > >> @BindingAnnotation > >> @Retention(RetentionPolicy.RUNTIME) > >> @Target(ElementType.LOCAL_VARIABLE) > >> public @interface Good { > >> } > > >> ================================ > >> Bad.java > >> ================================ > >> package net.jiakuan.test.sample4; > > >> import java.lang.annotation.ElementType; > >> import java.lang.annotation.Retention; > >> import java.lang.annotation.RetentionPolicy; > >> import java.lang.annotation.Target; > > >> import com.google.inject.BindingAnnotation; > > >> @Retention(RetentionPolicy.RUNTIME) > >> @BindingAnnotation > >> @Target(ElementType.LOCAL_VARIABLE) > >> public @interface Bad { > >> } > > >> ================================ > >> PlayerModule.java > >> ================================ > >> package net.jiakuan.test.sample4; > > >> import com.google.inject.Binder; > >> import com.google.inject.Module; > > >> 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); > >> } > >> } > > >> ================================ > >> PlayerClient.java > >> ================================ > >> package net.jiakuan.test.sample4; > > >> import com.google.inject.Guice; > >> import com.google.inject.Injector; > >> import com.google.inject.Module; > > >> public class PlayerClient { > >> public static void main(String[] args) { > >> PlayerModule module = new PlayerModule(); > >> Injector injector = Guice.createInjector(new Module[] { module }); > > >> �...@good > >> Player player = injector.getInstance(Player.class); > >> player.bat(); > >> player.bowl(); > >> } > >> } > > >> When I ran PlayerClient, the following error message was printed in > >> console, I could not find what was wrong with my code: > > >> ---------------------------------------------------------------------------------------------------------------------------------------------- > >> Exception in thread "main" com.google.inject.ConfigurationException: > >> Guice configuration errors: > > >> 1) No implementation for net.jiakuan.test.sample4.Player was bound. > >> while locating net.jiakuan.test.sample4.Player > > >> 1 error > >> at > >> com.google.inject.InjectorImpl.getProvider(InjectorImpl.java:784) > >> at > >> com.google.inject.InjectorImpl.getProvider(InjectorImpl.java:743) > >> at > >> com.google.inject.InjectorImpl.getInstance(InjectorImpl.java:793) > >> at net.jiakuan.test.sample4.PlayerClient.main(PlayerClient.java:18) > > >> ---------------------------------------------------------------------------------------------------------------------------------------------- > >> Anyone can help me? thanks in advance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "google-guice" 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?hl=en -~----------~----~----~----~------~----~------~--~---
