这是一个关于更好地将嵌入码头的代码与连接 servlet的代码分开的问题。
我正在尝试修改此示例代码,以便获得可运行的战争,即我可以放入现有 Jetty 容器的战争文件,或使用类似java -jar webapp-runnable.war
. 示例代码属于这两篇博文:No.1、No.2。
我遵循了GuiceServlet 手册并创建了一个web.xml
and 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
样子才能起作用?