Guice 学习(五)多接口的实现( Many Interface Implementation)
2024-09-05 13:19:31
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();
}
}
// 假设不小心一个属性绑定了多个接口怎么办? --》不能够绑定多个服务。
最新文章
- UnitOfWork以及其在ABP中的应用
- <;转>;MFC注册系统/全局热键。
- AJAX--XMLHttpRequest Object 知识整理
- ant exec
- Track files and folders manipulation in Windows
- Linux学习-0626
- Spring自学教程-IOC、DI、AOP(二)
- 进入IT行业四月后的感想(生活日志)欢迎评论
- POJ-1915 Knight Moves (BFS)
- [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups
- javascript高级程序设计第3版——第5章 引用类型
- Thymeleaf学习记录(1)--启动模板及建立Demo
- Xmind破解
- JVM性能调优2:JVM性能调优参数整理
- BZOJ 3963 HDU3842 [WF2011]MachineWorks cdq分治 斜率优化 dp
- BZOJ.4919.[Lydsy1706月赛]大根堆(线段树合并/启发式合并)
- daterangepicker 使用方法总结
- 51Nod 1433 0和5(9的倍数理论)
- 【LeetCode】001. TwoSum
- Python的工具包[0] ->; numpy科学计算 ->; numpy 库及使用总结