我需要一个关于如何使用Google-guice为服务编写多个实现的建议。下面是示例使用Guice使用提供者的服务的多个实现
TestService testService =new TestServiceImplOne();
TestService testService =new TestServiceImplTwo();
作为吉斯不允许类型结合到一个以上的实施方式中,如误差下面的代码结果
binderObject.bind(SomeType.class).to(ImplemenationOne.class);
binderObject.bind(SomeType.class).to(ImplemenationTwo.class);
我们可以与命名注解如下
解决这个binder.bind(Player.class).annotatedWith(Names.named("Good")).to(GoodPlayer.class);
binder.bind(Player.class).annotatedWith(Names.named("Bad")).to(BadPlayer.class);
@Named("Good") Player goodPlayer = (Player)injector.getInstance(Player.class);
@Named("Bad") Player badPlayer = (Player)injector.getInstance(Player.class);
但我工作的应用程序是这样的。我们结合在init()方法中的所有模块和创建的注射器模块:
//separate method to bind
protected void configure() {
bind(new TypeLiteral<List<Service>>() {}).toInstance(serviceSets);
}
//separate method to inject
Injector i = Guice.createInjector(modules);
但上述过程中,我可以一个实现类只是绑定到接口(服务类)
你能请为我提供一种与提供者一起完成此任务的方法。我想这样做下面这样
class TestServiceProvider extends Provider{
// some code where it returns the instance of impl class needed. In my case TestServiceImplOne and TestServiceImplTwo and provider returns the corresponding instance of service class
}
并绑定与提供者类的服务类。这样
bind(TestService.class).toProvider(TestServiceProvider.class);
的东西,如果有人建议使用提供商或者说我可以注入我想在客户端执行的任何其他方式一个很好的例子,我将不胜感激。
注意:我正在使用webservices,我不确定如何在向服务类调用webservice时注入不同的实现。
First of all thanks very much for responding . Coming straight to the point
Iam working on webservices . Heres's the Flow
//获取URI GET http://www.google.com:8182/indi/provide/organizations/ {}欧
OrganizationsResource -------->OrganizationService------>OrganizationServiceImpl
Iam binding OrganizationService with OrganizationServiceImpl and injecting the OrganizationService in OrganizationsResource
@Inject
public void setOrganizationService(OrganizationService orgService) {
this.orgService= orgService;
}
Its fine till here but i have two implementations for OrganizationService ------>OrgDeatilsServiceImpl which does some other job
Now i want to bind both OrganizationServiceImpl and OrgDeatilsServiceImpl to OrganizationService
Confusions:
1) What procedure i have to use in Guice to bind two implementaions?
2) How exactly i can code in OrganizationsResource to dynamically decide which implementation to call.
I would appreciate if you give a sample example for the above requirement.
这是绝对不清楚为什么你不能使用绑定注释。请在这一部分展开。顺便说一下你的注释示例是错误的。注释局部变量并用'Injector.getInstance()'调用赋值它们什么都不会做。你必须使用'Key'类。 –