简体   繁体   English

如何配置jetty与Guice和Vaadin一起运行

[英]How to configure jetty to run with Guice and Vaadin

I create guice servlet like this: 我像这样创建guice servlet:

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: 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. 问题是,当我运行jetty然后Guice创建servlet的实例(在控制台中打印“Test”)但是当我尝试在浏览器中运行应用程序时,我得到NPE并且在控制台“TTest”出现。

So jetty create another instance of servlet that is not managed by guice. 所以jetty创建了另一个不受guice管理的servlet实例。

Question is how to configure jetty to use only guice? 问题是如何配置jetty只使用guice?

You have to create a guice servlet module which extends com.​google.​inject.​servlet.ServletModule (let's call it FooModule ). 你必须创建它延伸的吉斯的servlet模块com.​google.​inject.​servlet.ServletModule (姑且称之为FooModule )。 You define there your bindings and paths to servlets by overriding configureServlets() method. 您可以通过重写configureServlets()方法来定义绑定和servlet路径。

Then you must create context listener by extending com.google.inject.servlet.GuiceServletContextListener (let's call it BarContextListener ). 然后,您必须通过扩展com.google.inject.servlet.GuiceServletContextListener (让我们称之为BarContextListener )来创建上下文侦听器。 There you must implement getInjector() method, with something like that: 在那里你必须实现getInjector()方法,类似的东西:

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: 然后从web.xml中删除所有servlet映射,然后放入过滤器:

   <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. 通过这一切,您的所有servlet都由Guice管理,并在您的应用程序的服务器端启用依赖项注入。 It works for me in Tomcat so it should also work on Jetty. 它在Tomcat中适用于我,所以它也适用于Jetty。 Don't forget to include guice-servlet-<version>.jar into your classpath. 不要忘记将guice-servlet-<version>.jar包含在类路径中。 I didn't use it with Vaadin, but I guess my answer helped You little bit. 我没有和Vaadin一起使用,但我想我的回答对你有所帮助。

I have written a blog post on the subject, I'm sure it would be most helpful : 我写了一篇关于这个主题的博客文章,我相信它会最有帮助:

Vaadin + GAE + Guice-Servlet - TUTORIAL Vaadin + GAE + Guice-Servlet - TUTORIAL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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