updating bindings at runtime?

1,436 views
Skip to first unread message

lahnsurfer

unread,
Mar 12, 2010, 2:49:16 AM3/12/10
to google-guice
Hello Guice users,

I would like to know if there's a way to update any bindings at
runtime. I would like to do this because i am currently using guice to
configure a mail module i have written. The mail module reads some
settings from a property file (like host, username, password, etc) and
the mail module binds these settings so they can be injected. Here's
some code:

String mailHost = configuration.getString(MAIL_HOST);
bind(String.class).annotatedWith(Names.named(MAIL_HOST)).toInstance(mailHost);

Now i want the user to be able to change these settings at runtime
through a preferences dialog.

After the user has updated the settings i want to update the bindings
so that all of the objects which received the settings are called with
the new setting. Is this possible? Or do you have another idea how to
solve this problem. Sure i could also inject the configuration itself,
but then i am coupled to this kind of configuration.

Thanks for you help!

Greetings
Tim

Bob Lee

unread,
Mar 12, 2010, 2:50:16 PM3/12/10
to google...@googlegroups.com
I'd use a provider method and make sure you don't cache the value anywhere.

Bob


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


bklough

unread,
Mar 12, 2010, 3:25:26 PM3/12/10
to google-guice
Could take another route and rather than worry about binding the
individual settings, encapsulate the settings into a MailSettings
class, create a Provider for MailSettings, then add the
Provider<MailSettings> as a dependency to your classes requiring that
information, or the MailSettings class itself as a dependency,
depending on usage. Added bonus of adding as many attributes as you
want down the road. If you can't modify those constructors/classes,
create a builder that uses MailSettings to populate the string
parameters.
Reply all
Reply to author
Forward
0 new messages