This looks to be exactly what the servlet extension<http://code.google.com/p/google-guice/wiki/Servlets>and the @RequestScope scope are for -- objects in that scope are only created once per request (and thereafter the cached version is used).
sam On Tue, Jan 3, 2012 at 11:37 AM, bpsm <[email protected]> wrote: > > I'm new to Guice and am stumped right now as how to properly use > Guice's facilities to accomplish what I need. > > The situation, simplified: > > - A depends on B and C > - B and C depend on D > - I'd like B and C to share a D since what D is providing is expensive > to compute and will be the same for both B and C. > > +--> [ B ]--+ > [ A ]---| |---> [ D ] > +--> [ C ]--+ > > - Each time my service S is called, it uses a graph of objects shaped > like this to respond to the request. > > This code is running running in s Spring container. The servlet-class > is configured to HttpRequestHandlerServlet. The service itself is > running as an HttpInvokerServiceExporter. (I'm new to Sping too, > though others in my team have more experience with it.) The whole mess > is running on JBoss. > > What I'd like: > > Every time I handled a call to my service, I request a new from A the > injector. This gets me a new B and a new C. It also gets me two new > Ds, but I only want one to avoid repeated work. > > (A, B, C are stateless, but D is mutable and use-once-per-request.) > > D as Singleton scope is not what I want since this would reuse D > across multiple service calls. I *guess* what I want is for D to be in > Request Scope and A, B, C to be unscoped, but I don't understand how > to accomplish this. I'm too much of a noob. > > I've read the docs on Scopes on the guice homepage and I'm going back > over the relevant sections in Google Guice book from APress, but I'm > not groking it yet. > > As a stop-gap I'm just declaring D as sigleton, but constructing a new > injector for each call. This is expensive, though not prohibitively so > because the module is not all that complex. I'm sure there's a better > way. Please enlighten me! > > thanks, > Ben > > > -- > 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. > > -- 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.
