For what it's worth, we instantiate our applications through Guice. Having
your application "go get" its Injector kind of violates the DI concept.

filter("/*").through(WicketFilter.class);
bind(WebApplication.class).to(CustomWebApplication.class);
bind(WicketFilter.class).to(CustomWicketFilter.class);

@Singleton
private static class CustomFilter extends WicketFilter {
@Inject private Provider<WebApplication> webApplicationProvider;
 @Override
protected IWebApplicationFactory getApplicationFactory() {
return new IWebApplicationFactory() {
@Override
public WebApplication createApplication(WicketFilter filter) {
return webApplicationProvider.get();
}
@Override
public void destroy(WicketFilter filter) {
}
};
}
}

On Thu, Oct 11, 2012 at 11:49 PM, Daniel Watrous
<[email protected]>wrote:

> Dan,
>
> I think you're right. Since in the WicketApplication init() function I
> attempt to get the bootStrapInjector like this:
>         Injector bootStrapInjector = (Injector)
> this.getServletContext().getAttribute(Injector.class.getName());
>
> I just can't figure out how to get the injector into the
> ServletContext before init() is run in my WicketApplication.
>
> Daniel
>
> On Wed, Oct 10, 2012 at 6:10 PM, Dan Retzlaff <[email protected]> wrote:
> > Daniel,
> >
> > What you're doing should work, but I think you're giving
> > your GuiceComponentInjector a null Injector. Unit tests don't go through
> > web.xml to set up its context listeners, so
> > your GuiceServletContextListener never has a chance to construct and
> > register an Injector with the ServletContext.
> >
> > Dan
> >
> > On Wed, Oct 10, 2012 at 5:30 PM, Daniel Watrous <
> [email protected]>wrote:
> >
> >> Hi,
> >>
> >> I've integrated Guice into Wicket successfully, but I'm struggling
> >> with the unittests. I'm not sure how to get the injector into my
> >> HomePage class. Here's my setup.
> >>
> >> I'm using GuiceFilter with a GuiceServletContextListener. That creates
> >> the injector and a ServletModule which defines the WicketApplication.
> >> I followed:
> >> http://code.google.com/p/google-guice/wiki/ServletModule
> >>
> >> Here's some of MyGuiceServletConfig extends GuiceServletContextListener
> >>
> >>     @Override
> >>     protected Injector getInjector() {
> >>         return Guice.createInjector(createServletModule(), new
> >> MongoHoneybadgerModule());
> >>     }
> >>
> >>     private ServletModule createServletModule() {
> >>         return new ServletModule() {
> >> ...
> >>
> >> In my WicketApplication extends WebApplication I have this init() method
> >>
> >>     @Override
> >>     public void init()
> >>     {
> >>         super.init();
> >>         Injector bootStrapInjector = (Injector)
> >> this.getServletContext().getAttribute(Injector.class.getName());
> >>         getComponentInstantiationListeners().add(new
> >> GuiceComponentInjector(this, bootStrapInjector));
> >>     }
> >>
> >> Now, in my HomePage.java class I have
> >>
> >> public class HomePage extends WebPage {
> >>     private static final long serialVersionUID = 1L;
> >>     @Inject private Injector injector;
> >>
> >>     public HomePage(final PageParameters parameters) {
> >>         super(parameters);
> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >>
> >>         add(new Label("version", myobj.getValue()));
> >>     }
> >> }
> >>
> >> This all runs great inside a web container as a servlet.
> >>
> >> The PROBLEM: I'm getting a NullPointerException on the line where I
> >> reference the injector:
> >>         SomeType myobj = injector.getInstance(SomeType.class);
> >>
> >> My test class is what was generated by the wicket quickstart. I'm not
> >> sure how to make an injector available in setUp.
> >>
> >>     @Before
> >>     public void setUp() {
> >>         tester = new WicketTester(new WicketApplication());
> >>     }
> >>
> >> Any ideas?
> >>
> >> Thanks,
> >> Daniel
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [email protected]
> >> For additional commands, e-mail: [email protected]
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

Reply via email to