2009/9/28 delightwjk <[email protected]>

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

^ here you're asking Guice for an instance of whatever is bound to
Player.class (no annotation)

Guice has no knowledge of the annotation on the local variable, because it
never sees it at all,
the JVM simply executes the getInstance method and assigns the result to the
local variable.
This is different to when Guice is injecting a field/constructor/method,
because it can see the
member that it is injecting the instance into.

Try using:

    Player player = injector.getInstance( Key.get( Player.class, Good.class
) );

to get a Good Player.

       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.
>
> >

-- 
Cheers, Stuart

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to