Guice 学习(五)多接口的实现( Many Interface Implementation)
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();
}
}
// 假设不小心一个属性绑定了多个接口怎么办? --》不能够绑定多个服务。
Guice 学习(五)多接口的实现( Many Interface Implementation)的更多相关文章
- hibernate 学习 五 hibernate核心接口
一 Configuration接口 Configuration对象只存在于系统的初始化阶段.配置相关. 配置文件可以使用默认的路径,也可以指定路径. Configuration config = ne ...
- (转)MyBatis框架的学习(五)——一对一关联映射和一对多关联映射
http://blog.csdn.net/yerenyuan_pku/article/details/71894172 在实际开发中我们不可能只是对单表进行操作,必然要操作多表,本文就来讲解多表操作中 ...
- TweenMax动画库学习(五)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- Guice学习(一)
Guice学习(一) Guice是Google开发的一个轻量级依赖注入框架(IOC).Guice非常小而且快,功能类似与Spring,但效率上网上文档显示是它的100倍,而且还提供对Servlet,A ...
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...
- AI-视图组件-五个接口的最终简化版
五个接口最终版 #url.py # 序列化最贱版本 url(r'^customer/$', views.CustomerView.as_view({"get":"list ...
- SVG 学习<五> SVG动画
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...
- Android JNI学习(五)——Demo演示
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- ZigBee学习五 无线温度检测
ZigBee学习五 无线温度检测 1)修改公用头文件GenericApp.h typedef union h{ uint8 TEMP[4]; struct RFRXBUF { unsigned cha ...
- cesium 学习(五) 加载场景模型
cesium 学习(五) 加载场景模型 一.前言 现在开始实际的看看效果,目前我所接触到基本上都是使用Cesium加载模型这个内容,以及在模型上进行操作.So,现在进行一些加载模型的学习,数据的话可以 ...
随机推荐
- 设计模式之笔记--简单工厂模式(Simple Factory)
简单工厂模式(Simple Factory) 类图 描述 简单工厂: 一个抽象产品类,可以派生多个具体产品类: 一个具体工厂类: 工厂只能创建一个具体产品. 应用场景 汽车接口 public inte ...
- WEB字体,多列布局和伸缩盒
WEB字体 语法 @font-face{ font-family:""; src:url() format() ... } 兼容性写法 @font-face { font-fami ...
- javascript的有效校验
//年月日期有效性检验 function yearAndMonthCheck() { var flag = true; var currentyear = new Date().getFullYear ...
- javascript中模仿块级作用域
学过 javascript 的都知道 javascript 里面没有块级作用域的概念,这就意味着在块语句中定义的变量,实际上是在包含函数中而非语句中创建的,看下面的例子: function outPu ...
- 新手学习selenium路线图(老司机亲手绘制)-学前篇【转载】
前言: 本来这篇是只在微信公众号(yoyoketang)上发布的,最近一搜,发现本很多人copy(copy公众号的,图片是加载不出来的)了,还没注明出处,不想多说什么,博客上也同步更新这篇吧! 最近群 ...
- beijing2016
4625: [BeiJing2016]水晶 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 201 Solved: 70[Submit][Status ...
- AC日记——[Ahoi2013]作业 bzoj 3236
3236 思路: 莫队+树状数组维护: 代码: #include <cmath> #include <cstdio> #include <cstring> #inc ...
- 【cocos2d-js官方文档】十七、事件分发机制
简介http://blog.csdn.net/qinning199/article/details/41951517 游戏开发中一个很重要的功能就是交互,如果没有与用户的交互,那么游戏将变成动画,而处 ...
- HDU 1181.变形课-并查集
变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submis ...
- 数据结构-二叉搜索树(BST binary search tree)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...