开发者

Guice and Servlet - General understanding problem?

I want to use Guice in a Vaadin app (but this should not be Vaadin specific).

In my Vaadin MyApp (extending application class) I can use the @Inject annotation to inject the Implementations defined in the module in my Custom ContextListener extending GuiceServletContextListener, where the Injector gets created.

I now want to access the Implementations from Everywhere in my app, but I always get NullPointerExceptions.

I thought its sufficient to create the Top Object (which would be the Application Class in mycase(?)) with guice so that every objects created from this class (in this case with new) can inject.

If I create every object with Injector.getInstance(..) it would work, but with this approach I have to create every "intermediate" class, which even does not use @Inject with this method.

Is what I'm hoping to do even possible with guice, or am I missing something? My main purpose is to access a DAOFactory Implementation (needs to be session scoped) from everywhere in my project without passing it to every single constructor.

Thanks, iamgalaxys

Some code I am using:

ApplicationServlet:

@Singleton
public class GuiceApplicationServlet extends AbstractApplicationServlet {

    private static final long serialVersionUID = -5729153715228068816L;

    // Guice stuff
    protected final Provider<Application> applicationProvider;
    @Inject
    public GuiceApplicationServlet(Provider<Application> applicationProvider) {
            this.applicationProvider = applicationProvider;
    }
    @Override
    protected Class getApplicationClass() throws ClassNotFoundException {
            return Application.class;
    }
    @Override
    protected Application getNewApplication(HttpServletRequest request) throws ServletException {
            return applicationProvider.get();
    }

ContextListener:

public class MyContextListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
ServletModule module = new ServletModule() {
开发者_运维技巧            @Override
            protected void configureServlets() {
...
serve("/*").with(GuiceApplicationServlet.class, params);

}

And I have defined the Guice Filter and ContextListener in web.xml


Guice only works in a servlet container if you have the necessary voodoo activated in your web.xml.

You might want to read the documentation about how to install and configure guice-servlet.jar - http://code.google.com/p/google-guice/wiki/Servlets


What do you mean by "so that every objects created from this class (in this case with new) can inject"? If you're instantiating objects in your Application class using new, then I think that's your problem. Essentially, Guice replaces new. You'll need to instantiate your service objects with Guice (either by injecting them, or using injector.getInstance()) in order for their members or constructors to be injected by Guice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜