@BindingAnnotation
(1) 如果 一个接口只有一个实现,使用这种连接注解就可以:
bind(XXInterface.class).to(XXImpl.class); @Inject
XXInterface xxInterface (2) 当一个接口由多个实现的时候,上面的@Inject根本不知道怎么绑定,这个时候可以使用自定义的绑定注解BindingAnnotation
。
public class AnimalModule extends AbstractModule {
@Override
protected void configure() {
bind(Animal.class).annotatedWith(Cat.class).toInstance(new Animal("Meow"));
bind(Animal.class).annotatedWith(Dog.class).toInstance(new Animal("Woof"));
}
@Provides
List<Animal> provideAnimalList(@Cat Animal cat, @Dog Animal dog) {
List<Animal> animals = new ArrayList<Animal>();
animals.add(cat);
animals.add(dog);
return animals;
}
public static void main(String[] args) {
List<Animal> animals = Guice.createInjector(new AnimalModule()).getInstance(Key.get(new TypeLiteral<List<Animal>>() {
}));
for (Animal animal : animals) {
System.out.println(animal);
}
}
}
Annotations : 如下的Cat和Dog是两个绑定注解。
@Retention(value = RetentionPolicy.RUNTIME)
@BindingAnnotation
public @interface Cat {
}
@Retention(value = RetentionPolicy.RUNTIME)
@BindingAnnotation
public @interface Dog {
}
Output :
Animal{sound='Meow'}
Animal{sound='Woof'}
@BindingAnnotation的更多相关文章
- java轻量级IOC框架Guice
Google-Guice入门介绍(较为清晰的说明了流程):http://blog.csdn.net/derekjiang/article/details/7231490 使用Guice,需要添加第三方 ...
- Effective Java 56 Adhere to generally accepted naming conventions
Typographical naming conventions Identifier Type Type Examples Package com.google.inject, org.joda.t ...
- Google Guice结合模式
于Guice于,喷油器装配工作是一个对象图,当请求类型实例,喷油器根据推断对象如何映射到创建的实例.解决依赖.要确定如何解决的依赖就需要配置喷油器结合的方式. 要创建绑定(Binding)对象,能够继 ...
- Effective Java 第三版——68. 遵守普遍接受的命名约定
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- Guice 依赖绑定
Guice 依赖绑定 连接绑定(Linked Bingdings) 连接绑定是 Guice 最基本的一种绑定方式.这种绑定方式我们需要在自己定义的 Module 的 configure() 中编写绑定 ...
- Google-Guice入门介绍
原地址:http://blog.csdn.net/derekjiang/article/details/7231490 一. 概述 Guice是一个轻量级的DI框架.本文对Guice的基本用法作以介绍 ...
- 解读超轻量级DI容器-Guice与Spring框架的区别【转载】
依赖注入,DI(Dependency Injection),它的作用自然不必多说,提及DI容器,例如spring,picoContainer,EJB容器等等,近日,google诞生了更轻巧的DI容器… ...
- Guice 学习(五)多接口的实现( Many Interface Implementation)
1.接口 /* * Creation : 2015年6月30日 */ package com.guice.InterfaceManyImpl; public interface Service { p ...
- Guice 4.1教程
Guice是Google开发的一个开源轻量级的依赖注入框架,运行速度快,使用简单. 项目地址:https://github.com/google/guice/ 最新的版本是4.1,本文基于此版本. 0 ...
随机推荐
- SSH服务:packet_write_wait: Connection to 67.218.143.160 port 22: Broken pipe错误处理
1.在~/.ssh/config配置文件中添加 IPQoS lowdelay throughput 2.在/etc/ssh/ssh_config配置文件中添加 IPQoS lowdelay throu ...
- jQuery 滑动选项卡jQuery tabulous.js
A jQuery tabs module for todays web! 实例DEMO 运行一下 Documentation Tabulous.js can be used with any cont ...
- DOM 踩踩踩
1.如果是想给一个DOM元素添加一个伪类,可以转换为 为这个元素添加一个类名,这个类名上面绑定一个伪类. 2.append一个元素,删除掉原来的元素再进行添加.
- React-Native基础_5.列表视图ListView
列表视图ListView 用来显示垂直滚动列表,需要指定两个东西,1 数据的来源 dataSource,2 渲染列表的条目布局 rendRow 'use strict' import React, { ...
- Lua基础---一维数组与多维数组
Lua语言中,数组和C还是有区别的,Lua的数组下标从1开始计数,而C语言的数组下标从0开始计数,我想这可能是设计Lua的人想要符合人的思维习惯而去这么设计的. 数组,也就是按相同类型,在内存中顺序排 ...
- hdu 1098
http://acm.hdu.edu.cn/showproblem.php?pid=1098 假设x=m时,65|f(m),即65|5*m^13+13*m^5+k*a*m 计算f(m+1)=(5*m^ ...
- iOS通讯录相关知识-浅析
本文来自于:贞娃儿的博客 http://blog.sina.com.cn/zhenwawaer 在开发一些应用中,我们如果需要iPhone设备中的通讯录信息.或者,需要开发通讯录相关的一些功能.那 ...
- Apache下配置Openstack Horizon (转)
非常详尽的Horizon配置介绍,转自 dev.cloudwatt.com Deploy Horizon from source with Apache and SSL Some companies ...
- C++ 回调函数的几种策略
Stackoverflow中提出了这样一个问题:假设我们实现了一个User类,Library类,现在Library类中utility需要回调User中func方法,总结答案,常见的几种方法如下: 静态 ...
- HihoCoder 1075 开锁魔法III(概率DP+组合)
描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它.初始时,崔克茜将会随机地选择 k 个盒子用魔法将它 ...