Guice 学习(五)多接口的实现( Many Interface Implementation)
来源:互联网 发布:阿里云docker镜像仓库 编辑:程序博客网 时间:2024/06/05 02:58
1、接口
/* * Creation : 2015年6月30日 */package com.guice.InterfaceManyImpl;public interface Service { public void execute();}
2、两个实现类
package com.guice.InterfaceManyImpl;public class OneService implements Service { @Override public void execute() { System.out.println("Hello! I'M Service 1!"); }}
package com.guice.InterfaceManyImpl;public class TwoService implements Service { @Override public void execute() { System.out.println("Hello! I'M Service 2!"); }}
3、两个注解类
package com.guice.InterfaceManyImpl;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import com.google.inject.BindingAnnotation;@Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.FIELD, ElementType.PARAMETER })@BindingAnnotationpublic @interface One { }
package com.guice.InterfaceManyImpl;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import com.google.inject.BindingAnnotation;@Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.FIELD, ElementType.PARAMETER })@BindingAnnotationpublic @interface Two {}
4、多接口实现测试类
package com.guice.InterfaceManyImpl;import com.google.inject.Binder;import com.google.inject.Guice;import com.google.inject.Inject;import com.google.inject.Module;/* * 接口的多实现: * 此类的结构是注入了两个service服务,注解One和OneService关联,第二个和它一样 */public class InterfaceManyImpl { @Inject @One private Service oneService; @Inject @Two private Service twoService; public static void main(String[] args) { InterfaceManyImpl instance = Guice.createInjector(new Module() { @Override public void configure(Binder binder) { //让注解类和实现类绑定 binder.bind(Service.class).annotatedWith(One.class).to(OneService.class); binder.bind(Service.class).annotatedWith(Two.class).to(TwoService.class); } }).getInstance(InterfaceManyImpl.class); instance.oneService.execute(); instance.twoService.execute(); }}
5、无注解的多接口实现测试类
package com.guice.InterfaceManyImpl;import com.google.inject.Binder;import com.google.inject.Guice;import com.google.inject.Inject;import com.google.inject.Module;import com.google.inject.name.Named;import com.google.inject.name.Names;/** * TODO : 程序员比较懒,不想写注解来区分多个服务则可以使用Google提供的一个叫Names的模板来生成注解 * @author E468380 */public class NoAnnotationMultiInterfaceServiceDemo { @Inject @Named("One") private static Service oneService; @Inject @Named("Two") private static Service twoService; public static void main(String[] args) { Guice.createInjector(new Module() { @Override public void configure(Binder binder) { // 这里不同 binder.bind(Service.class).annotatedWith(Names.named("One")).to(OneService.class); binder.bind(Service.class).annotatedWith(Names.named("Two")).to(TwoService.class); binder.requestStaticInjection(NoAnnotationMultiInterfaceServiceDemo.class); } }); NoAnnotationMultiInterfaceServiceDemo.oneService.execute(); NoAnnotationMultiInterfaceServiceDemo.twoService.execute(); }}
6、静态的多接口实现测试类
问题(1)静态注入多个服务怎么写?
package com.guice.InterfaceManyImpl;import com.google.inject.Binder;import com.google.inject.Guice;import com.google.inject.Inject;import com.google.inject.Module;/** * TODO :也可以静态注入多个服务 * * @author E468380 */public class StaticMultiInterfaceServiceDemo { @Inject @One private static Service oneService; @Inject @One private static Service twoService; public static void main(String[] args) { Guice.createInjector(new Module() { @Override public void configure(Binder binder) { binder.bind(Service.class).annotatedWith(One.class).to(OneService.class); binder.bind(Service.class).annotatedWith(Two.class).to(TwoService.class); binder.requestStaticInjection(StaticMultiInterfaceServiceDemo.class); } }); StaticMultiInterfaceServiceDemo.oneService.execute(); StaticMultiInterfaceServiceDemo.twoService.execute(); }}// 如果不小心一个属性绑定了多个接口怎么办? --》不可以绑定多个服务。
0 0
- Guice 学习(五)多接口的实现( Many Interface Implementation)
- 接口(Interface)和实现(implementation)
- 接口的实现(interface)
- 区分 inheritance of interface(接口继承)和 inheritance of implementation(实现继承)
- 设计模式:Interface Inheritance (接口继承)和 Implementation Inheritance(实现继承)
- Guice学习(一)
- 显示接口成员实现(explicit interface member implementation)总结
- C++ - 接口继承(interface) 和 实现继承(implementation) 详解
- Google Guice 接口多实现 注入
- [翻译] Effective C++, 3rd Edition, Item 34: 区分 inheritance of interface(接口继承)和 inheritance of implementation(实现继承)(上)
- [翻译] Effective C++, 3rd Edition, Item 34: 区分 inheritance of interface(接口继承)和 inheritance of implementation(实现继承)(下)
- com学习(五)——实现多接口
- hibernate 学习五——多对多(Many-to-Many)
- 接口(Interface)的用途
- 接口(五)实现接口
- 接口(interface)和接口的注意事项
- C++接口实现总结(Interface)
- Lua 接口(Interface)实现方式之一
- PHP实现HTTP断点续传下载的方法
- sudo+user+命令 提示user不在sudoers文件中的解决办法
- Linux基础——刘晓涛
- Xcode 无证书打包ipa
- 分页技术
- Guice 学习(五)多接口的实现( Many Interface Implementation)
- 控制器和多功能设备
- java.lang.IllegalStateException解决
- SIFT特征点算法源码分析(opencv)
- 实习日志3---看图猜名字界面实现,以及基本的一些要求实现
- Understanding Cubert Concepts(二)Co-Partitioned Blocks
- ADSP-TS201-Chapter1-Architecture
- Linux下C程序开发环境--刘晓涛
- 微信开发 java