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

  1. java轻量级IOC框架Guice

    Google-Guice入门介绍(较为清晰的说明了流程):http://blog.csdn.net/derekjiang/article/details/7231490 使用Guice,需要添加第三方 ...

  2. Effective Java 56 Adhere to generally accepted naming conventions

    Typographical naming conventions Identifier Type Type Examples Package com.google.inject, org.joda.t ...

  3. Google Guice结合模式

    于Guice于,喷油器装配工作是一个对象图,当请求类型实例,喷油器根据推断对象如何映射到创建的实例.解决依赖.要确定如何解决的依赖就需要配置喷油器结合的方式. 要创建绑定(Binding)对象,能够继 ...

  4. Effective Java 第三版——68. 遵守普遍接受的命名约定

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  5. Guice 依赖绑定

    Guice 依赖绑定 连接绑定(Linked Bingdings) 连接绑定是 Guice 最基本的一种绑定方式.这种绑定方式我们需要在自己定义的 Module 的 configure() 中编写绑定 ...

  6. Google-Guice入门介绍

    原地址:http://blog.csdn.net/derekjiang/article/details/7231490 一. 概述 Guice是一个轻量级的DI框架.本文对Guice的基本用法作以介绍 ...

  7. 解读超轻量级DI容器-Guice与Spring框架的区别【转载】

    依赖注入,DI(Dependency Injection),它的作用自然不必多说,提及DI容器,例如spring,picoContainer,EJB容器等等,近日,google诞生了更轻巧的DI容器… ...

  8. Guice 学习(五)多接口的实现( Many Interface Implementation)

    1.接口 /* * Creation : 2015年6月30日 */ package com.guice.InterfaceManyImpl; public interface Service { p ...

  9. Guice 4.1教程

    Guice是Google开发的一个开源轻量级的依赖注入框架,运行速度快,使用简单. 项目地址:https://github.com/google/guice/ 最新的版本是4.1,本文基于此版本. 0 ...

随机推荐

  1. SpringInAction--条件化的Bean

    学习了profile bean之后,发现有的时候bean还是有根据环境来选择的余地的,那么假设我们希望某个bean只有当另外某个特定的bean也声明了之后才会创建.我们还可能要求只有某个特定的环境变量 ...

  2. SpringInAction-- 配置Profile Bean

    Profile Bean 使用场景描述: 在开发软件的时候,在数据库方面,往往不是一个库就能解决的,一般分为开发库.测试库.生产库,在这些库设置链接的时候,也会配置其对应的数据. 现有一种方式,就是单 ...

  3. New Concept English there (4)

    20w/m These days, people who do manual work often receive far more money than people who work in off ...

  4. New Concept English there (9)

    31 65% Cats never fail to fascinate human beings. They can be friendly and affectionate towards huma ...

  5. 疑问:@Autowired的作用?[待解答]

    有下面一个Spring的工程,工程结构如下: 代码如下: applicationContext.xml: <?xml version="1.0" encoding=" ...

  6. c# 文件日志处理 需要log4net配置

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  7. ASP.NET后台怎么输出方法中间调试信息?

    后台方法,不止是aspx.cs,而是页面调用的一些其它方法.想调试这些方法,我以前winform都是MessageBox.Show一些中间结果,现在我也想用这种方式.但想想,网页会触发 Message ...

  8. Java基础面试题 (一)

    1.面向对象的三个特征 封装,继承,多态.这个应该是人人皆知,有时候也会加上抽象. 2.多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消息 ...

  9. using中StreamWriter XmlWriter 区别

    使用StreamWriter using (var writer = new StreamWriter(File.Create(path))) { writer.WriteLine("sdf ...

  10. 对Json的各种遍历方法

    慎用for in函数(有可能由于原型链的问题导致遍历问题): 如果要是用for in  一定要使用if (obj1.hasOwnProperty(key)) {}先做判断 解决方法 :1.eval() ...