Guice can only inject objects that it either creates itself or is asked to inject. At a bare minimum, you'll need to get a copy of the injector and call its "injectMembers" method, passing it your Foo object.
Given your current setup, one way to do this would be to add these lines to your init method: Injector injector = (Injector) initialConfig.getServletContext.getAttribute(Injector.class.getName()); injector.injectMembers(foo); However, this depends on implementation details of GuiceServletContextListener, and in general is not how it is designed to be used. GuiceServletContextListener is designed to be used in conjunction with GuiceFilter and ServletModule, which should be configured to create your servlets and the objects they depend on. I recommend you read the Guice Servicet Extensions documentation. http://code.google.com/p/google-guice/wiki/Servlets On Tue, Jul 5, 2011 at 11:29 AM, 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.
