It looks like you're angling to tell Guice, "I only want a single instance
of SettingsXmlReader." That's what the Singleton annotation/scope is for.
You can either add @Singleton to the SettingsXmlReader implementation class
or you can explicitly bind it as a singleton (see my above example that
bound it as a singleton). That tells Guice to only ever create one
instance.
FWIW, if the classes that need the getXYZ() method are only injecting
SettingsXmlReader in order to get the result of its getXYZ method, I
recommend that you abstract away that dependency. Classes should only ever
inject their direct dependencies. In this case, SettingsXmlReader isn't
really a dependency -- it's just a stop on the way to the right dependency.
Let's say getXYZ returns a Map<String, Object> -- you can add a
@Provides Map<String, Object> xyz(SettingsXmlReader sxr) { return
sxr.getXYZ() }
to your module, and then classes will never need to know or care that the
XYZ map is actually constructed by a settings reader. That lets you write
smarter tests (you can test your classes now without having to insert SXR
into the mix) and is clearer when looking at the class about what it really
needs.
(You can add niceties liked binding annotations, too. For example the
@Provides method could have a return signature of "@Provides
@Named("SettingsMap") Map<String, Object>" and classes can inject a
@Named("SettingsMap") Map<String, Object>.)
Sam
On Tue, Apr 27, 2010 at 8:41 AM, Patrick Bergner <[email protected]> wrote:
> Thanks for your reply, Sam. With (a) you're absolutely right. However,
> I'm not sure if I understood (b) correctly. From the way I understood
> it the answer would be that that's not what I need. Let my try to
> explain SettingsXmlReader a bit better:
>
> (i) SettingsXmlReader has dependencies in its constructor that are
> passed in by Guice.
>
> (ii) SettingsXmlReader has a load() method that does the reading of
> the actual .xml file. This takes time and should therefore only happen
> once during application lifetime. The read settings are then stored in
> a Map.
>
> (iii) SettingsXmlReader has getXYZ() methods that shall return the
> values of the .xml file once it has been read. These methods are
> called multiple times from multiple classes.
>
> The crux is that I only want to read the .xml once and need the
> instance of SettingsXmlReader where the .xml has been read multiple
> times in multiple classes.
>
> What I wanted to do is bind(SettingsXmlReader.class).toInstance(sxr)
> in my AppModule but in order to create the instance sxr I need the
> injector created with AppModule (to resolve the dependencies
> SettingsXmlReader has).
>
> Maybe that's what you just explained and I did not get it yet.
>
> Patrick
>
> On Apr 27, 2:16 pm, Sam Berlin <[email protected]> wrote:
> > 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?
>
> --
> 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.