Hi, I am trying to achieve some binding with Guice 2.0. I did search the
mailing list but couldn't find a similar problem/solution.
I am trying to bind different implementations of same interface. I want to
discriminate them against their annotations.
These are my classes to be injected dependencies to, I do not want to change
them a lot:
class SingleRunner {
@Inject
public void setExecutor(@Named("single") ExecutorService executor) {
// ...
}
}
class PoolRunner {
@Inject
public void setExecutor(@Named("pool") ExecutorService executor) {
// ...
}
}
This is the traditional approach to bind providers for these classes;
creating to provider classes and bind them:
class SingleProvider implements Provider<ExecutorService> {
@Inject
private SomeOtherDependency other;
public ExecutorService get() {
return singleExecutorService;
}
}
class PoolProvider implements Provider<ExecutorService> {
@Inject
private SomeOtherDependency other;
public ExecutorService get() {
return poolExecutorService;
}
}
class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(ExecutorService.class).annotatedWith(Names.named("pool")).toProvider(PoolProvider.class);
bind(ExecutorService.class).annotatedWith(Names.named("single")).toProvider(SingleProvider.class);
}
}
But I am not comfortable with creating two provider classes. I want one
provider class to provide all instances, ie something like that:
class AllExecutorProvider {
@Inject
public void setSomeOtherDependency(SomeOtherdependency dep) {
// ...
}
public ExecutorService getExecutor(String name) {
if (name.equals("pool")) {
return poolExecutor;
} else if (name.equals("single")) {
return singleExecutor;
} else {
return null;
}
}
}
Thanks in advance
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.