Guice系列之用户指南(二)
原文地址:https://code.google.com/p/google-guice/wiki/BindingAnnotations
BindingAnnotations(绑定注释):一个类型可能会有多个实现类,在绑定时加上注解,这样可以确定在依赖注入时用的具体是哪个实现类。
具体有2种,一种是自定义注解,另一种是@Named。
先看自定义注解,贴代码:
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
@BindingAnnotation
@Target({FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
public @interface Tom {
}
public interface Animal {
void say();
}
public class Cat implements Animal{
@Override
public void say() {
System.out.println("i am a cat");
}
}
public class Dog implements Animal{
@Override
public void say() {
System.out.println("i am a dog");
}
}
public interface HelloService {
void sayHello();
}
import com.google.inject.Inject;
public class HelloServiceImpl implements HelloService {
Animal animal;
@Inject
public HelloServiceImpl(@Tom Animal animal) {
this.animal = animal;
}
@Override
public void sayHello() {
System.out.println(animal.getClass().getSimpleName());
animal.say();
}
}
import com.google.inject.AbstractModule;
public class HelloServiceModule extends AbstractModule {
@Override
protected void configure() {
bind(Animal.class).annotatedWith(Tom.class).to(Cat.class);
bind(Animal.class).to(Dog.class);
bind(HelloService.class).to(HelloServiceImpl.class);
}
}
public class Test {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new HelloServiceModule());
HelloService helloService = injector.getInstance(HelloService.class);
helloService.sayHello();
}
}
发现,多个实现类绑定到一个类型时,后者覆盖前者。
第二种,用@Named注解,贴代码:
import com.google.inject.name.Named;
public class HelloServiceImpl2 implements HelloService {
Animal animal;
@Inject
public HelloServiceImpl2(@Named("Snoopy") Animal animal) {
this.animal = animal;
}
@Override
public void sayHello() {
System.out.println(animal.getClass().getSimpleName());
animal.say();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
public class HelloServiceModule2 extends AbstractModule {
@Override
protected void configure() {
bind(Animal.class).annotatedWith(Names.named("Snoopy")).to(Dog.class);
bind(Animal.class).to(Cat.class);
bind(HelloService.class).to(HelloServiceImpl2.class);
}
}
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Test2 {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new HelloServiceModule2());
HelloService helloService = injector.getInstance(HelloService.class);
helloService.sayHello();
}
}
《Jersey用户指南》–序言 此书是Jersey 2.23.1的用户手册。我们努力及时更新这本手册因为Jersey 2.23.1增加了一些新的功能。当您在阅读这本用户手册的时候, 请同时参考我们的Jersey API 手册从而进一步理解Jersey 的功能和API。
Guice是谷歌推出的一个轻量级依赖注入框架,帮助我们解决Java项目中的依赖注入问题。如果使用过Spring的话,会了解到依赖注入是个非常方便的功能。不过假如只想在项目中使用依赖注入,那么引入Spring未免大材小用了。
相关文章
- 2022最新ES面试题整理(Elasticsearch面试指南系列)「建议收藏」
- 快速完成MySQL安装指南(如何安装mysql安装版)
- Linux下摄像头驱动安装指南(linux下摄像头驱动)
- 成功插入Oracle数据库:一步一步指南(插入oracle数据库)
- 优化Linux对UDP丢包优化指南(udp丢包linux)
- 免费下载:Linux技术手册全套指南(linux技术手册下载)
- Linux下高效刻录命令实战指南(linux 刻录命令)
- Oracle新人入职查询指南(oracle入职查询)
- 单机模式下的Redis分区实施指南(单机redis如何分区)
- Oracle数据库参数修改指南(oracle修改参数语句)