Using Guice Servlet, you can and should inject the services a servlet
depends on it into it. You aren't constrained to providing a no-arg
constructor as with normal servlets, so you can use a constructor for this.
Your example should be modified to look like this:
@Singleton
class BarServlet extends HttpServlet {
private final Foo foo;
@Inject public BarServlet(Foo foo) {
this.foo = foo;
}
}
Since the Foo is created and injected by Guice, it will have its
dependencies injected as expected.
--
Colin
On Tue, Jul 5, 2011 at 2:29 PM, thnguyen <[email protected]> wrote:
> The object with the @Inject setter is instantiated within a servlet.
> The servlet is configured in web.xml.
>
> Example:
>
> class Foo {
> String attrib;
>
> @Inject
> public void setAttrib(String attrib) { this.attrib = attrib }
> }
>
> class BarServlet extends HttpServlet {
> Foo foo;
>
> public void init(ServletConfig initialConfig) {
> foo = new Foo();
> }
> }
>
> On Jun 30, 8:03 pm, Christopher Currie <[email protected]> wrote:
> > How and where is the object with the @Inject setter being injected?
> > You've not specified, in your description, how this is supposed to
> > happen.
> >
> >
> >
> >
> >
> >
> >
> > On Thu, Jun 30, 2011 at 4:32 PM, thnguyen <[email protected]> wrote:
> > > Can someone assist me in getting DI working? Here's what I've done in
> > > my webapp:
> >
> > > 1) Added method injection to a setter:
> >
> > > @Inject
> > > public void setSearchApplication(SearchApplication searchApp) {
> > > this.searchApp = searchApp;
> > > }
> >
> > > 2) Extended a AbstractModule and overrode configure():
> >
> > > protected void configure() {
> >
> > >
> bind(SearchApplication.class).to(LuceneSearch.class).in(Scopes.SINGLETON);
> > > }
> >
> > > 2) Extended GuiceServletContextListener and overrode getInjector():
> >
> > > @Override
> > > protected Injector getInjector() {
> > > return Guice.createInjector(Stage.DEVELOPMENT, new
> > > ExtendedAbstractModule());
> > > }
> >
> > > 3) Registered the listener in web.xml:
> >
> > > <listener>
> > > <listener-class>com.inject.ExtendedGuiceServletContextListener</
> > > listener-class>
> > > </listener>
> >
> > > I've verified that the
> > > ExtendedGuiceServletContextListener.getInjector() is being called
> > > after starting the webapp. I then check the value of searchApp in the
> > > class that contains the setter that is @Inject annotated as mentioned
> > > in step 1 above. The value of searchApp is null. Is there a step I
> > > am missing?
> >
> > > --
> > > 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 athttp://
> 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.