这是一个关于更好地分离嵌入码的代码代码接线servlets的问题。与jetty + guice运行的战争
我正在尝试改编this sample code,这样我就会得到一个可运行的战争,即可以放入现有Jetty容器的战争文件,或者使用类似java -jar webapp-runnable.war
的命令运行独立战争。 示例代码属于以下两篇博文:No.1,No.2。
我跟着GuiceServlet manual创建了一个web.xml
和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 [email protected]{/,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]
这里是我的代码。如前所述,我从this repo on github开始。
1)我提取的匿名内部类型AbstractModule从com.teamlazerbeez.http.HttpServerMain
,放入一个新的类com.teamlazerbeez.http.HttpServerModule
。这个类现在在创建Guice Injector in HttpServerMain
(l36)时实例化。
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
在这样的它们描述的设置过程变得可用e到我的GuiceServletConfig
? GuiceServletConfig
看起来像这个工作?