第三章 【第六节】无目标绑定UntargettedBindings:Guice指导教程(原创翻译)

目录
[隐藏]

UntargettedBindings

创建没有目标的绑定
Creating bindings that don’t have targets

无目标绑定 Untargeted Bindings

你可以创建没有特别指定的目标的绑定。这点对具体的类或者类型比较有用,他们需要用到@ImplementedBy和@ProvidedBy注解。无目标绑定把一个类通知给注入器,这样可以对依赖进行预处理。无目标绑定没有语法条款,类似如下写法:
You may create bindings without specifying a target. This is most useful for concrete classes and types annotated by either @ImplementedBy or @ProvidedBy. An untargetted binding informs the injector about a type, so it may prepare dependencies eagerly. Untargetted bindings have no to clause, like so:

    bind(MyConcreteClass.class);
    bind(AnotherConcreteClass.class).in(Singleton.class);

当指定绑定注解,你必须添加目标绑定,即使是相同的具体类。例如:
When specifying binding annotations, you must still add the target binding, even it is the same concrete class. For example:

    bind(MyConcreteClass.class)
        .annotatedWith(Names.named("foo"))
        .to(MyConcreteClass.class);
    bind(AnotherConcreteClass.class)
        .annotatedWith(Names.named("foo"))
        .to(AnotherConcreteClass.class)
        .in(Singleton.class);


实例:

UntargettedBindings(没有目标的绑定):就是在module里绑定时不需要明确实现类,结合用@ImplementedBy或者@ProvidedBy实现。区别绑定的注解时要加上实现类的类型,即使是父类型。

import com.google.inject.ImplementedBy;
 
@ImplementedBy(value = Cat.class)
public interface Animal {
    void say();
}
 
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
 
public class AnimalModule extends AbstractModule {
 
    @Override
    protected void configure() {
        bind(Animal.class)
            .annotatedWith(Names.named("Tom"))
            .to(Animal.class)
            .in(Singleton.class);
        System.out.println("run AnimalModule.configure()");
    }
}
 
public class Cat implements Animal {
    @Override
    public void say() {
        System.out.println("i am a cat");
    }
}
 
import javax.inject.Named;
import com.google.inject.Inject;
 
public class CatWrapper {
     
    Animal animal;
     
    @Inject
    private CatWrapper(@Named("Tom") Animal animal) {
        System.out.println("CatWrapper.Contructor()");
        this.animal = animal;
    }
     
    public void say() {
        animal.say();
    }
     
    public Animal getAnimal() {
        return this.animal;
    }
}
 
import com.google.inject.Guice;
import com.google.inject.Injector;
 
public class Test {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new AnimalModule());
         
        CatWrapper wrapper1 = injector.getInstance(CatWrapper.class);
        System.out.println(wrapper1);
        System.out.println(wrapper1.getAnimal());
         
        CatWrapper wrapper2 = injector.getInstance(CatWrapper.class);
        System.out.println(wrapper2);
        System.out.println(wrapper2.getAnimal());
    }
}

执行结果:
run AnimalModule.configure()
CatWrapper.Contructor()
com.zoo.guice.userguide.chap6.p1.CatWrapper@8fce95
com.zoo.guice.userguide.chap6.p1.Cat@143c8b3
CatWrapper.Contructor()
com.zoo.guice.userguide.chap6.p1.CatWrapper@12d7a10
com.zoo.guice.userguide.chap6.p1.Cat@143c8b3

上面这种是@ImplementedBy加上注解绑定的,后面也跟上了to,下面这种是@ProvidedBy实现。

import com.google.inject.ProvidedBy;
 
@ProvidedBy(value = AnimalProvider.class)
public interface Animal {
    void say();
}
 
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
 
public class AnimalModule2 extends AbstractModule {
 
    @Override
    protected void configure() {
        bind(Animal.class).in(Singleton.class);
        System.out.println("run AnimalModule.configure()");
    }
}
 
import com.google.inject.Provider;
 
public class AnimalProvider implements Provider<Animal>{
 
    public Animal get() {
        System.out.println("run Provider.get()");
        return new Cat();
    }
}
 
public class Cat implements Animal {
    @Override
    public void say() {
        System.out.println("i am a cat");
    }
}
 
import com.google.inject.Guice;
import com.google.inject.Injector;
 
public class Test2 {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new AnimalModule2());
        Animal cat_A = injector.getInstance(Animal.class);
        System.out.println(cat_A);
        Animal cat_B = injector.getInstance(Animal.class);
        System.out.println(cat_B);
    }
}

执行结果:
run AnimalModule.configure()
run Provider.get()
com.zoo.guice.userguide.chap6.p2.Cat@18ee9d6
com.zoo.guice.userguide.chap6.p2.Cat@18ee9d6

下一节:ToConstructorBindings


说明:

      鉴于网上guice中文资料较少,出于个人爱好,对该项目下的用户API文档进行翻译。如有翻译不恰当之处,还望指正。

      google Guice 项目地址:https://github.com/google/guice

      Guice 英文API地址:https://github.com/google/guice/wiki/LinkedBindings

分享到:

发表评论

昵称

沙发空缺中,还不快抢~