On Mon, Jan 23, 2012 at 6:35 AM, ich du <[email protected]> wrote:

> i try to sort up the deployment of my webapp. so i need to override
> contextInitilaized in GuiceServletContextListener. there i need to check
> the state of some module-bound objects.
> but i get problems:
> i field-injected the needed objects (a logger and a external datasource)
> and tried to use them in contextInitialized but i get NullPointerExceptions
> all injected stuff is null in scope of contextInitialized (later, after
> deployment all injections work properly). the problem is that all guice
> injection starts within contextInitialized:
> "super.contextInitialized(servletContextEvent);"
>
> so how to let guice inject the fields (or do i need another kind of
> injection?) after "super.contextInitialized(servletContextEvent);"?
>

Does your code look like this?

    public class Main extends GuiceServletContextListener {

@Override protected Injector getInjector() {
    return Guice.createInjector(stage,
new MyServletModule(),
// ... other modules ...
new AbstractModule() {
    @Override protected void configure() {
requestInjection(Main.this);
    }
}
    ));
}

@Override public void contextInitialized(ServletContextEvent event) {
    super.contextInitialized(event);
    // use someField here
}

@Inject volatile SomeType someField;
    }

This pattern works for me. If it doesn't work for you, I'd like to know why
not.

--tim

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