简体   繁体   中英

How to configure jetty to run with Guice and Vaadin

I create guice servlet like this:

public class GuiceApplicationServlet extends AbstractApplicationServlet {

    protected Provider<Application> applicationProvider;

    public GuiceApplicationServlet() {
        System.out.println("TTest");
    }

    @Inject
    public GuiceApplicationServlet(Provider<Application> applicationProvider) {
        super();
        this.applicationProvider = applicationProvider;
        System.out.println("Test");
    }

    @Override
    protected Class<? extends Application> getApplicationClass()
            throws ClassNotFoundException {
        return Application.class;
    }

    @Override
    protected Application getNewApplication(HttpServletRequest request)
            throws ServletException {
        return applicationProvider.get();
    }
}

web.xml:

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>pl.koziolekweb.vaadin.guice.servlet.VaadinGuiceConfiguration</listener-class>
    </listener>


    <servlet>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <servlet-class>pl.koziolekweb.vaadin.guice.servlet.GuiceApplicationServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

The problem is that when I run jetty then Guice create instance of servlet (print "Test" in console) but when I try to run application in browser i get NPE and in console "TTest" appear.

So jetty create another instance of servlet that is not managed by guice.

Question is how to configure jetty to use only guice?

You have to create a guice servlet module which extends com.​google.​inject.​servlet.ServletModule (let's call it FooModule ). You define there your bindings and paths to servlets by overriding configureServlets() method.

Then you must create context listener by extending com.google.inject.servlet.GuiceServletContextListener (let's call it BarContextListener ). There you must implement getInjector() method, with something like that:

protected Injector getInjector() {
        Injector injector = Guice.createInjector(new FooModule());
        return injector;
    }

Then you remove all servlet mappings from your web.xml, and then put filter:

   <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

and context listener You created:

<listener>
        <listener-class>path.to.package.of.context.listener.BarContextListener</listener-class>
    </listener>

By this all Your servlets are managed by Guice and enable dependency injection in server side of Your application. It works for me in Tomcat so it should also work on Jetty. Don't forget to include guice-servlet-<version>.jar into your classpath. I didn't use it with Vaadin, but I guess my answer helped You little bit.

I have written a blog post on the subject, I'm sure it would be most helpful :

Vaadin + GAE + Guice-Servlet - TUTORIAL

因为Guice应该创建所有的Servlet实例,所以必须从web.xml文件中删除servletservlet-mapping条目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM