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,现在进行一些加载模型的学习,数据的话可以 ... 
随机推荐
- TCP的可靠性
			原因: 1.确认和重传机制 2.序列号 3.流量控制(窗口) 4.拥塞控制(慢启动,拥塞避免,快速重传,快速恢复) http://blog.csdn.net/baidu_35692628/articl ... 
- JDK 动态代理 源码简单分析
			代理的作用就是在访问真实对象之前或者之后可以额外加入一些操作. JDK 的动态代理 只需要 5 步. 真实对象必须要实现接口,首先创建一个接口 public interface HelloWorld ... 
- solr in action
			Miscellaneous settings: Lucene version solr使用luceneMatchVersion来决定当前索引正在使用的lucene版本及是否禁用新版本的特性. 
- docker从零开始 存储(二)volumes 挂载
			使用volumes 卷是保存Docker容器生成和使用的数据的首选机制.mount binds依赖于主机的目录结构,而卷完全由Docker管理.卷绑定安装有几个优点: 与绑定装入相比,卷更易于备份或迁 ... 
- node中通过orm2链接mysql的一个坑
			代码是orm上的例子,出现如下错误: ORMError: Connection protocol not supported - have you installed the database dri ... 
- 运行微信支付demo
			首先要说说写这篇文章的初衷:集成支付宝支付运行demo都是可以正常运行的,但是我下载下来微信支付的demo,却发现一大堆报错,而且相关文章几乎没有,可能大家觉得没必要,也许你觉得很简单:但是技术大牛都 ... 
- 浅谈C#多线程与UI响应
			www.educity.cn 发布者:shenywww 来源:网络转载 发布日期:2014年10月06日 ... 
- ZCMU Problem G: 素数对(数论,素数筛法)
			#include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #inc ... 
- 51nod 1137 矩阵乘法【矩阵】
			1137 矩阵乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个N * N的矩阵M1和M2,输出2个矩阵相乘后的结果. Input 第1行 ... 
- 51nod 循环数组最大子段和(动态规划)
			循环数组最大子段和 输入 第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9) 输出 输 ... 
