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); }}
阅读全文
0 0
- Guice Names的模板来生成注解
- Guice Providers注解和Names模板
- Guice注解(单例)
- Guice provider注解
- Guice 注解@Provides
- 通过Annotation注解来生成Bean
- 利用注解来生成HELLO WORLD
- Google Guice 注入(@Inject注解)
- android 注解 guice.jar ,roboguice.jar 使用
- Google Guice 注入(@Inject注解)
- Google Guice 注入(@Inject注解)
- Guice的scope
- Guice的使用例子
- guice的使用例子
- spring&guice 的AOP
- Guice bindInterceptor的使用
- 使用宏来生成类模板
- guice
- Codeforces 217A Ice Skating 题解
- GITC 2017全球互联网技术大会 上海站日程抢先看!
- AVL树
- NumberPicker
- libevent 学习----------尾队列 tail queue
- Guice Names的模板来生成注解
- KVO
- android:给图片打水印
- extern "C" 解析
- java String中的compareTo
- OkHttp的简单使用
- SPOOLing系统
- Kotlin的第一行代码
- POI读取excel某个单元格内容