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. Java坦克大战 (四) 之子弹的产生

    本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...

  2. xdebug参数说明

    ;;;;;;;;;;;;;;;;;;;;;;;;; Basic Features; xdebug基本功能,如堆栈跟踪,递归错误安全输出,时间内存跟踪等;;;;;;;;;;;;;;;;;;;;;;;;; ...

  3. 十个技巧快速优化你的Laravel 5 程序

    性能一直是 Laravel 框架为人诟病的一个点,所以调优 Laravel 程序算是一个必学的技能. 接下来分享一些开发的最佳实践,还有调优技巧,大家有别的建议也欢迎留言讨论. 这里是简单的列表: 配 ...

  4. React-Native集成dva.js

    dvajs作为一个基于redux的状态管理框架,在react中的表现还是很不错的,如果我们想要在react-native应用中使用dvajs该怎么做呢? 首先安装dva-core和react-redu ...

  5. [转]认识session

    今天想用一个session来实现用户登录判断,也算是对之前session的探究,查了下资料session的运行机制如下: session是服务器端的一种会话机制,当客户端的请求服务器创建一个sessi ...

  6. HDU 2102 A计划【三维BFS】

    A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  7. HDU 1251 统计难题 (字典树)(查询是否为前缀)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. HDU1754I Hate It(线段树)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. boost::Circular Buffer

    boost.circular_buffer简介 很多时候,我们需要在内存中记录最近一段时间的数据,如操作记录等.由于这部分数据记录在内存中,因此并不能无限递增,一般有容量限制,超过后就将最开始的数据移 ...

  10. Placement new的用法及用途【转】

    什么是placement new? 所谓placement new就是在用户指定的内存位置上构建新的对象,这个构建过程不需要额外分配内存,只需要调用对象的构造函数即可.举例来说: class foo{ ...