Let me repeat the problem to see if I understand it.  You

a) Have some kind of SettingsXmlReader class that reads an XML file and
requires dependencies from Guice passed to its constructor (or parameter
methods).

b) Need to also tell Guice to bind the result of some of SettingsXmlReader's
methods (let's say: SXR.getFoo & SXR.getBar) so that other objects created
by Guice can have those injected.

Is that correct?

If so, you can use Provider bindings to accomplish what you want.  For
example

class MyModule extends AbstractModule() {
  public void configure() {
     bind(SettingsXmlReader.class).in(Scopes.SINGLETON);
  }

  @Provides Foo foo(SettingsXmlReader sxr) {
      return sxr.getFoo();
   }

   @Provides Bar bar(SettingsXmlReader sxr) {
      return sxr.getBar();
    }
 }

That tells Guice that whenever something wants a 'Foo', it should call the
'foo' method in MyModule and for 'Foo' it'll hand off whatever that method
returns.  Internally to Guice, it knows that the 'foo' method has a
dependency on SettingsXmlReader, so it'll pass the SettingsXmlReader that is
bound through Guice.  Same thing with Bar.

Sam

On Tue, Apr 27, 2010 at 8:07 AM, Patrick Bergner <[email protected]> wrote:

> Thanks for your replies. They give me some idea of the "Guice way".
> I'll make the utilities non-static and let Guice create the instance
> for me.
>
> Now, if you don't mind, give me a hint on this one: I have a Settings
> object that is a wrapper for a settings.xml file. I instantiate it
> using Guice. As reading the .xml takes time it should only happen once
> during the execution of the program, but I need the values (that
> should be accessed through the wrapper object once it has loaded
> the .xml content) multiple times.
>
> The trouble (in my head) now is that I cannot bind() an instance of
> the Settings wrapper in my Guice module because I need the injector
> that will be created using just this module for creating the instance.
>
> How can this be adressed?
>
> --
> 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]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>

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