I think there's a bit of chicken-and-egg confusion happening here - 
specifically:  Modules are things which set up bindings, which can *later* 
be used for injection.  The injector, which does the injecting, is 
*created* using the modules.

It looks like you are trying to get things injected *into* your module, 
which doesn't really make sense, because the injector that would do that 
doesn't exist yet, and won't exist until your module exists - by which time 
it is already constructed (yes, you could create an injector that creates 
some more modules and then create an injector over them so it's turtles all 
the way down, but these sorts of contortions become unreadable quickly).

Think of it this way:  Guice adds an explicit "initialization" phase to 
your application - think of it like running a meta-constructor for your 
whole application.  Modules' configure() methods are the code that runs 
inside that constructor.

If you need to load some configuration that will affect how things are 
bound - you need to pass some arguments into that "meta-constructor" - then 
you need some mechanism (such as reading a file, looking at environment 
variables, parsing command-line arguments - whatever suits you), but it's 
not going to be injection because the injector doesn't exist yet.

A straightforward form of this would be to do something like:
Properties p = //load some properties file
for (String key : p.stringPropertyNames()) {
   bind 
(String.class).annotatedWith(Names.named(key)).toInstance(p.getProperty(key));
   // or bindConstant or whatever
}

but has the nasty side-effect that if a property is missing from the file 
and some object needs it, a runtime exception will be thrown, probably when 
you don't expect it.  The library I pointed you at, at its heart, does more 
or less what the code above does, but with provisions for providing default 
values, standard filesystem locations for the file, automatic refresh, and 
ways to namespace classes or packages to load from different files/urls.

The point is, it sounds like you're looking for something to be injected 
into a module, but at the time your module is created, the injector does 
not exist - and while there might be hacky ways to convince Guice to do 
something like that, it's probably not a good idea - better to keep it 
simple.

-Tim

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to