On 10/08/10 14:23, tflobbe wrote:
Hi Willi, my problem is that I need a different implementation of the
QueryExecutor depending on the value of
Configuration.getProperty("executor.queryExecutor"), and that value
can change on runtime. If I just use:
Ah ok, i got it now.

Try in your module:

@Provides
public QueryExecutor getExecutor(Configuration config, Injector injector) {
    final String name = config.getProperty("executor.queryExecutor");
final Key<QueryExecutor> key = Key.get(QueryExecutor.class, Names.named(name));
    return injector.getInstance(key);
}

An depend on a Provider<QueryExecutor> in your classes.

Hope that helps.
@Inject
public MyService(@Named("one") QueryExecutor executor) {
...

then the implementation can't be modified on runtime, am I right?


On Aug 9, 3:45 pm, Willi Schönborn<[email protected]>
wrote:
On 09/08/2010 15:55, tflobbe wrote:

Hi All, I have the exact same problem as Tim in message
http://groups.google.com/group/google-guice/browse_thread/thread/ee24...
I tried using a provider method but it doesn't work either. I'm doing
something like this:
@Provides
public QueryExecutor createQueryExecutor() {
    Key<QueryExecutor>    executorKey = Key.get(QueryExecutor.class,
Names.named(Configuration.getProperty("executor.queryExecutor")));
    Provider<QueryExecutor>    provider = getProvider(executorKey);
    return provider.get();
}
On the same module, on the "configuration" method I bind the interface
"QueryExecutor" to diferent implementations depending on the "Named"
annotation.
bind(QueryExecutor.class).annotatedWith(Names.named("one")).to(QueryExecutorImpl1.class);
bind(QueryExecutor.class).annotatedWith(Names.named("two")).to(QueryExecutorImpl2.class);
Looks like your are binding using "one" and "two" but you try to
retrieve it using "executor.queryExecutor".
What's the use of that provides method anyway?
Can't you just use the following?

@Inject
public MyService(@Named("one") QueryExecutor executor) {
      ...

}
Can I use the provider from the provider method? It seems that the
binder is null when the createQueryExecutor() method is called and
that's causing a null pointer exception.
Thanks
Tom s

--
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.

Reply via email to