0

这是一个关于更好地将嵌入码头的代码与连接 servlet的代码分开的问题。

我正在尝试修改此示例代码,以便获得可运行的战争,即我可以放入现有 Jetty 容器的战争文件,或使用类似java -jar webapp-runnable.war. 示例代码属于这两篇博文:No.1No.2

我遵循了GuiceServlet 手册并创建了一个web.xmland GuiceServletContextListener(见下文),但它们似乎并没有让我很了解mvn jetty:run; 当我尝试跑步时mvn jetty:run,我收到以下错误:

[main] DEBUG org.eclipse.jetty.util.log - Logging to Logger[org.eclipse.jetty.util.log] via org.eclipse.jetty.util.log.Slf4jLog
WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.m.p.JettyWebAppContext@3cceafcb{/,file:/[...]/src/main/webapp/,STARTING}{file:/[...]/src/main/webapp/}
com.google.inject.ConfigurationException: Guice configuration errors:||1) Explicit bindings are required and com.google.inject.servlet.InternalServletModule$BackwardsCompatibleServletContextProvider is not explicitly bound.|  while locating com.google.inject.servlet.InternalServletModule$BackwardsCompatibleServletContextProvider||1 error
    at [stack strace clipped]

这是我的代码。如前所述,我从github 上的这个 repo开始。

1)我从中提取了 AbstractModule 类型的匿名内部类com.teamlazerbeez.http.HttpServerMain并将其放入一个新类com.teamlazerbeez.http.HttpServerModule中。这个类现在在创建 Guice Injector 时被实例化HttpServerMain(l36)中创建 Guice Injector 时实例化此类中创建 Guice Injector 时实例化此类。

2)我的web.xml

<?xml version="1.0" ?>
<web-app
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    metadata-complete="false"
    version="3.0">

    <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>com.teamlazerbeez.http.GuiceServletConfig</listener-class>
    </listener>
</web-app>

3)我的com.teamlazerbeez.http.GuiceServletConfig

public class GuiceServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        //I know this code not come close to doing what I want, but I just don't know where to start
        return Guice.createInjector(new HttpServerModule());
    }

}

我的问题:我如何重构该HttpServerMain main方法,并HttpServerModule以使他们描述的设置过程对我有用的方式GuiceServletConfig?它必须是什么GuiceServletConfig样子才能起作用?

4

2 回答 2

1

我接受了@Alex建议,忘记了创建一个standalone-runnable war,而是专注于创建一个在guice ServletModulewar中完成大部分连接的工作。

为此,我更改了 myHttpServerModule以扩展 a ServletModule(而不是AbstractModule)并将大部分HttpServerMain.main()逻辑放在其configureServlets()方法中:

public class HttpServerModule extends ServletModule {
    @Override
    protected void configureServlets() {
        MetricRegistry metricRegistry = new MetricRegistry();
        bind(MetricRegistry.class).toInstance(metricRegistry);

        install(new SandwichModule());
        install(new JerseyMetricsModule());

        JmxReporter reporter = JmxReporter.forRegistry(metricRegistry).build();
        reporter.start();
    }
}

您会注意到一些HttpServerMain.main()逻辑现在不再出现在 java 代码中。这是因为现在web.xml会处理这些事情。

我的web.xmlGuiceServletConfig保持不变,我现在可以使用mvn jetty:run command. 现在可以删除 HttpServerMain,因为它不再做任何有用的事情。

于 2014-03-11T11:12:06.927 回答
1

我从来没有像罐子一样打过战争,所以我总是选择两种解决方案之一。但是,在使用战争时,设置一个在您的 IDE 中工作的嵌入式 Jetty 服务器并不难。为此,您可以使用 aWebAppContext来设置它web.xml。有关示例,请参阅此文档。从那里开始,一切都应该按照 Guice 网站上的建议进行。

但是,这不会创建可运行的战争(如java -jar yourapp.war),因为 jar 具有不同的内部布局。但是,如果您愿意,可以使用jetty-runner来解决此问题java -jar jetty-runner.jar yourapp.war

于 2014-02-12T12:57:37.010 回答