Guice Names的模板来生成注解

来源:互联网 发布:公众号运营知乎 编辑:程序博客网 时间:2024/06/06 08:36
package com.ilucky.guice.test6;import com.google.inject.Guice;import com.google.inject.Injector;import com.google.inject.Key;import com.google.inject.name.Names;/** * v1.0:20161114 * v1.0:20161116 * Names模板和Named注解. * 在Java的语言中,一个接口可以有多个实现类,基于这个思想,Guice提供了注解的方式,实现一个类型绑定多个实现, * 但是使用这种方式,会导致有很多注解类,如果不想写注解类区分服务,怎么办呢?Google帮我们提供了Names的模板来生成注解. */public class MainTest {    public static void main(String[] args) {        Injector injector = Guice.createInjector(new PersonModule());        /**         Key类的用途:         Binding key consisting of an injection type and an optional annotation. Matches the type and annotation at a point of injection.          For example, Key.get(Service.class, Transactional.class) will match:            @Inject           public void setService(@Transactional Service service) {             ...           }        */        Person teacher = (Person)injector.getInstance(Key.get(Person.class, Names.named("Teacher")));        teacher.say("teacher......");        Student student = (Student)injector.getInstance(Key.get(Person.class, Names.named("Student")));        student.say("student......");        //依赖注入:        System.out.println("------------------------------------------");        PersonCaller pc = injector.getInstance(PersonCaller.class);        pc.say();    }}/**Teacher=teacher......Student=student......------------------------------------------Teacher=tea...Student=std...如果报如下错误:------------------------------------------Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:1) No implementation for com.ilucky.druid.guice.test3.Student annotated with @com.google.inject.name.Named(value=Student) was bound.  while locating com.ilucky.druid.guice.test3.Student annotated with @com.google.inject.name.Named(value=Student)    for field at com.ilucky.druid.guice.test3.PersonCaller.student(PersonCaller.java:6)  while locating com.ilucky.druid.guice.test3.PersonCaller原因是PersonCaller类的对象类型有问题:@Inject@Named("Teacher")private Teacher teacehr;@Inject@Named("Student")private Student student; */
package com.ilucky.guice.test6;import com.google.inject.Binder;import com.google.inject.Module;import com.google.inject.name.Names;public class PersonModule implements Module {    public void configure(Binder binder) {        binder.bind(Person.class).annotatedWith(Names.named("Teacher")).to(Teacher.class);        binder.bind(Person.class).annotatedWith(Names.named("Student")).to(Student.class);    }}
package com.ilucky.guice.test6;import com.google.inject.Inject;import com.google.inject.name.Named;public class PersonCaller {    @Inject    @Named("Teacher")    private Person teacehr;    //private Teacher teacehr;    @Inject    @Named("Student")    private Person student;    //private Student student;    public void say() {        teacehr.say("tea...");        student.say("std...");    }}
package com.ilucky.guice.test6;public interface Person {    public void say(String say);}
package com.ilucky.guice.test6;public class Student implements Person  {    public void say(String say) {        System.out.println("Student="+say);    }}
package com.ilucky.guice.test6;public class Teacher implements Person {    public void say(String say) {        System.out.println("Teacher="+say);    }}