AnnotationDemo2 demo2 = new AnnotationDemo2(); System.out.println(demo2.inter);
maybe try AnnotationDemo2 demo2 = injector.getInstance(AnnotationDemo2.class); System.out.println(demo2.inter); Cheers Alen On 18 avg., 16:12, 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 -~----------~----~----~----~------~----~------~--~---
