You can inject the injector, if you really want to:

http://code.google.com/p/google-guice/wiki/ServletRegexKeyMapping#Injecting_the_injector

However, I find that this breaks down the abstraction that Guice
provides. I like for my servlet modules to install other modules that
are required for the servlet to run:

public class ContainerModule extends ServeletModule {

    protected void configureServlets() {

        install(new DatabaseModule());
        install(new LoggingModule());
        install(new ApplicationModule());

        serve("/pattern").with(MyServlet.class);
    }

}

If you have multiple servlets, then you can have multiple servlet
modules, that are all injected by a top-level "server" module (which
doesn't even need to be a ServetModule) that manages all the bindings
that are either shared or should only happen once per server, such as
JNDI, etc.

Some people don't like cross-module dependencies, so you might not
like this approach, but I find it easier to reason about, and you can
always enforce your cross-module dependencies by checking the results
of Binder.getProvider() in a Module's configure method.

HTH,
Christopher

On Fri, Apr 29, 2011 at 11:33 AM, Christopher Piggott
<[email protected]> wrote:
> I've been taking related resources and grouping them ... and creating
> multiple modules for various things, to keep them more "together" as a
> functional unit that can be added or removed.  I then create my
> Injector with:
>
>                injector = Guice.createInjector(new MainServletModule(),
>                                new ResourceModule1(),
>                                new OtherModule1(),
>                                new ResourceModule2());
>
> etc.
>
> I find it helps me to keep things straight by keeping the module in
> the same package as all the things being bound.
>
> I think it's really a matter of personal taste, but this approach has
> made me happier.
>
> --C
>
>
>
> On Fri, Apr 29, 2011 at 2:21 PM, garey <[email protected]> wrote:
>> Hi -
>>
>>    Still trying to figure out the best way to use Guice writing
>> webapps. Should I put my bindings, for example a JNDI connection
>> provider, into the servlet module, or should I write another module
>> for that type of bind? And if I do, how should I provide the injector
>> for that module to my servlets?
>>
>>
>> Or am I completely confused?
>>
>> Garey Mills
>>
>> --
>> 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.
>
>

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