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

时间:2023-12-05 09:54:26

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(); } }
// 假设不小心一个属性绑定了多个接口怎么办? --》不能够绑定多个服务。