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)的更多相关文章

  1. hibernate 学习 五 hibernate核心接口

    一 Configuration接口 Configuration对象只存在于系统的初始化阶段.配置相关. 配置文件可以使用默认的路径,也可以指定路径. Configuration config = ne ...

  2. (转)MyBatis框架的学习(五)——一对一关联映射和一对多关联映射

    http://blog.csdn.net/yerenyuan_pku/article/details/71894172 在实际开发中我们不可能只是对单表进行操作,必然要操作多表,本文就来讲解多表操作中 ...

  3. TweenMax动画库学习(五)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  4. Guice学习(一)

    Guice学习(一) Guice是Google开发的一个轻量级依赖注入框架(IOC).Guice非常小而且快,功能类似与Spring,但效率上网上文档显示是它的100倍,而且还提供对Servlet,A ...

  5. Guice 学习(六)使用Provider注入服务( Provider Inject Service)

    1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...

  6. AI-视图组件-五个接口的最终简化版

    五个接口最终版 #url.py # 序列化最贱版本 url(r'^customer/$', views.CustomerView.as_view({"get":"list ...

  7. SVG 学习<五> SVG动画

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  8. Android JNI学习(五)——Demo演示

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  9. ZigBee学习五 无线温度检测

    ZigBee学习五 无线温度检测 1)修改公用头文件GenericApp.h typedef union h{ uint8 TEMP[4]; struct RFRXBUF { unsigned cha ...

  10. cesium 学习(五) 加载场景模型

    cesium 学习(五) 加载场景模型 一.前言 现在开始实际的看看效果,目前我所接触到基本上都是使用Cesium加载模型这个内容,以及在模型上进行操作.So,现在进行一些加载模型的学习,数据的话可以 ...

随机推荐

  1. linux 中解析命令行参数(getopt_long用法)

    linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...

  2. 新版谷歌浏览器设置flash插件不提示步骤

    1.先去chrome实验室界面chrome://flags/#enable-ephemeral-flash-permission选择取消Disabled.取消该实验室选项. 2.然后去chrome:/ ...

  3. word-wrap

    平时的网页制作中碰到过这样的情况,比如说在blog中制作了一个完美而且又靓丽的评论布局,让你的用户浏览网页是可以给你添加评论,但当有人发布了一个原始网址或者其它超长的文本时,你此时的布局就被他们给彻底 ...

  4. java使用maven项目(二)分模块开发

    1       整合ssh框架 1.1     依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar, 这种情况 叫 依赖传递 1.2     依赖版本冲突的解决 1.  第 ...

  5. 【集合类型的并发】Collections.synchronizedList

    摘要: 详细的解析:Collections.synchronizedList :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedList ...

  6. 在servlet中返回json数据

    在servlet: String name = new tring(request.getParameter("name").getBytes("iso8859-1&qu ...

  7. [linux]多线程下载

    axel -n 10 -o /tmp/ http://soft.vpser.net/lnmp/lnmp0.7-full.tar.gz # 10是线程数

  8. flutter 频道切换

    https://github.com/flutter/flutter/wiki/Flutter-build-release-channels 频道说明页 https://flutter.dev/doc ...

  9. Python3 list与循环练习(购物车)

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author;Tsukasa product_list = [ ('Iphone',5800), (' ...

  10. yum保留安装包方法,以及存放路径

    修改/etc/yum.conf里面的参数keepcache,从0改为1. 安装包保存在/var/cache/yum文件夹下