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 })
@BindingAnnotation
public @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 })
@BindingAnnotation
public @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(); } }
// 假设不小心一个属性绑定了多个接口怎么办? --》不能够绑定多个服务。

最新文章

  1. UnitOfWork以及其在ABP中的应用
  2. <转>MFC注册系统/全局热键。
  3. AJAX--XMLHttpRequest Object 知识整理
  4. ant exec
  5. Track files and folders manipulation in Windows
  6. Linux学习-0626
  7. Spring自学教程-IOC、DI、AOP(二)
  8. 进入IT行业四月后的感想(生活日志)欢迎评论
  9. POJ-1915 Knight Moves (BFS)
  10. [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups
  11. javascript高级程序设计第3版——第5章 引用类型
  12. Thymeleaf学习记录(1)--启动模板及建立Demo
  13. Xmind破解
  14. JVM性能调优2:JVM性能调优参数整理
  15. BZOJ 3963 HDU3842 [WF2011]MachineWorks cdq分治 斜率优化 dp
  16. BZOJ.4919.[Lydsy1706月赛]大根堆(线段树合并/启发式合并)
  17. daterangepicker 使用方法总结
  18. 51Nod 1433 0和5(9的倍数理论)
  19. 【LeetCode】001. TwoSum
  20. Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结

热门文章

  1. Linux编写Shell脚本
  2. Mac-配置SecureCRT
  3. Ubuntu16.04 PPA方式安装JDK1.8
  4. python 去掉所有空白字符【解决】
  5. UVA12096 集合栈计算机(map和vector实现双射关系+集合的交并运算的STL)
  6. 21、Django实战第21天:课程章节信息
  7. 小型Web应用扫描工具Grabber
  8. Problem D: 零起点学算法24——判断奇偶数
  9. FCL研究-目录
  10. 搭建SSH框架–使用篇