Note how you *never* use the injector and you still are using the 'new' keyword.
Change the line:
AnnotationDemo2 demo2 = new AnnotationDemo2();
to:
AnnotationDemo2 demo2 = injector.getInstance(AnnotationDemo2.class);
On Tue, Aug 18, 2009 at 4:12 PM, zhaoyi<[email protected]> wrote:
>
> Below is my code. I want to inject the "inter" field. Why is the
> output null?
>
> import static java.lang.annotation.ElementType.FIELD;
> import static java.lang.annotation.ElementType.METHOD;
> import static java.lang.annotation.ElementType.PARAMETER;
> import static java.lang.annotation.RetentionPolicy.RUNTIME;
>
> import java.lang.annotation.Retention;
> import java.lang.annotation.Target;
>
> import com.google.inject.AbstractModule;
> import com.google.inject.BindingAnnotation;
> import com.google.inject.Guice;
> import com.google.inject.Inject;
> import com.google.inject.Injector;
>
> public class AnnotationDemo2 {
>
> �...@inject @MyAnnotation public Inter inter;
>
> public static void main(String[] args) {
> Injector injector = Guice.createInjector(new AbstractModule(){
> �...@override
> protected void configure() {
>
> bind(Inter.class).annotatedWith(MyAnontation.class).to
> (Type.class);
> }
> });
> AnnotationDemo2 demo2 = new AnnotationDemo2();
> System.out.println(demo2.inter);
>
> }
> }
>
> @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention
> (RUNTIME)
> @interface MyAnnotation{
>
> }
>
> interface Inter{
>
> }
>
> class Type implements Inter{
>
> }
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---