Guice注解实现一个类型绑定多个实现.
来源:互联网 发布:流程 数据库设计 编辑:程序博客网 时间:2024/06/17 12:48
package com.ilucky.guice.test5;import com.google.inject.Guice;import com.google.inject.Injector;import com.google.inject.Key;/** * v1.0:20161116 * 注解实现一个类型绑定多个实现. * 在Java的语言中,一个接口可以有多个实现类,基于这个思想,Guice提供了注解的方式,实现一个类型绑定多个实现. */public class MainTest { public static void main(String[] args) { Injector injector = Guice.createInjector(new PersonModule()); Person teacher = (Person)injector.getInstance(Key.get(Person.class, TeacherAnno.class)); teacher.say("teacher......"); Student student = (Student)injector.getInstance(Key.get(Person.class, StudentAnno.class)); student.say("student......"); }}/**Teacher=teacher......Student=student......*/
package com.ilucky.guice.test5;import com.google.inject.Binder;import com.google.inject.Module;public class PersonModule implements Module { public void configure(Binder binder) { binder.bind(Person.class).annotatedWith(TeacherAnno.class).to(Teacher.class); binder.bind(Person.class).annotatedWith(StudentAnno.class).to(Student.class); }}
package com.ilucky.guice.test5;public interface Person { public void say(String say);}
package com.ilucky.guice.test5;public class Student implements Person { public void say(String say) { System.out.println("Student="+say); }}
package com.ilucky.guice.test5;public class Teacher implements Person { public void say(String say) { System.out.println("Teacher="+say); }}
package com.ilucky.guice.test5;import static java.lang.annotation.RetentionPolicy.RUNTIME;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.Target;import com.google.inject.BindingAnnotation;@Target({ ElementType.TYPE, ElementType.METHOD })@Retention(RUNTIME)@BindingAnnotationpublic @interface StudentAnno {}
package com.ilucky.guice.test5;import static java.lang.annotation.RetentionPolicy.RUNTIME;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.Target;import com.google.inject.BindingAnnotation;@Target({ ElementType.TYPE, ElementType.METHOD })@Retention(RUNTIME)@BindingAnnotationpublic @interface TeacherAnno {}
阅读全文
0 0
- Guice注解实现一个类型绑定多个实现.
- 使用@Named注解绑定多个实现(java,scala)
- 如何实现为一个网卡绑定多个IP地址
- apache2虚拟主机实现一个服务器绑定多个域名
- spring注解识别一个接口的多个实现类
- Google Guice 接口多实现 注入
- JSP、PHP、ASP跳转代码实现一个网站空间绑定多个域名,建立多个网站
- 使用@Named注解(一个接口多个实现,匹配引入想要的实现类)
- spring注解 实现一个接口多个实现类的区分
- mysql实现绑定多个ip
- 实现一个注解接口
- linux下如何实现为一个网卡绑定多个IP地址
- 用.htaccess实现一个网站空间建N个网站 多域名绑定
- 转 -- Linux下实现一个网卡绑定多个IP地址
- SpringMVC3,使用RequestMappint的Param参数,实现一个url绑定多个方法
- 报表填报时,如何实现多个单元格绑定一个字段?
- 为阿里云ECS服务器二级域名绑定tomcat子目录,实现一个IP多个二级域名
- 一个虚拟空间,不用子目录绑定功能实现建多站(一个空间绑定多个域名实现自动跳转的几种方法!)
- 17-JSCPC&&CCPC中南赛区 H-Highway 树的直径DFS
- 发布-订阅模式(观察者模式)
- 只具备内网的服务器通过yum安装软件
- P1047 校门外的树
- Android常用检查判断方法
- Guice注解实现一个类型绑定多个实现.
- 升级linux内核(2.6.32->3.10.105),安装docker
- 上机第十四周实验
- 今日头条lite:你关心的才是头条【极乐小程序商店出品】
- 在java web项目中调用c++程序简单案例
- 20170522Link memo
- C++关于子类调用父类的构造方法的问题
- XMLElement,XMLDocument 用法
- 初探并发