Creating new bindings inside a provider?

4 views
Skip to first unread message

unguiculus

unread,
Nov 24, 2009, 9:17:29 AM11/24/09
to google-guice
Hello,

I am loading a Configuration object in a provider. The class
"Configuration" basically is a Map<String, String>. It has to be
loaded in a provider method. The point now is that I am not really
interested in binding the whole Configuration object. I'd rather bind
each config item. So, I tried the following:

@Provides
Configuration getConfiguration(...) {
Configuration config = ... // load instance here
Names.bindProperties(binder(), config);
return config;
}

However, this fails because the binder has already been reset at this
point with the call to "binder()" returning null.

Is it somehow possible to do something like this?

Thanks,
Reinhard

unguiculus

unread,
Nov 26, 2009, 11:32:04 AM11/26/09
to google-guice
Let me try and rephrase this. This is certainly not a common case. It
is actually more complicated than sketched in my original post.

We have a multi-threaded application. Each thread runs a job, which
has its own configuration. A job may even change configuration items
at runtime. The configuration object itself is bound in a custom
thread scope. This must happen in a provider method. Static binding in
the configure method is not possible, so I cannot do something like
Names.bindProperties() either. Also, each config item would have to be
bound such that it uses the configuration object's provider in order
to get the value using the right scope.

What I can do is bind certain properties explicitly, but I just want
to have this dynamic.

@Provides
@Named("myProp")
String getMyProp(final Provider<Configuration> configuration) {
return configuration.get().get("myProp");
}

I want to be able to inject a value using e. g. @Named("myProp") and
tell Guice that it should be looked up using the configuration
object's provider without having to write a provider method for each
config item. Would it be possible to implement something that does
that? Any pointers are appreciated.

Thanks,
Reinhard
Reply all
Reply to author
Forward
0 new messages