2014-02-09 126 views
0

这是一个关于更好地分离嵌入码的代码代码接线servlets的问题。与jetty + guice运行的战争

我正在尝试改编this sample code,这样我就会得到一个可运行的战争,即可以放入现有Jetty容器的战争文件,或者使用类似java -jar webapp-runnable.war的命令运行独立战争。 示例代码属于以下两篇博文:No.1,No.2

我跟着GuiceServlet manual创建了一个web.xmlGuiceServletContextListener(见下文),但他们似乎没有让我走得很远,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()); 
    } 

} 

我的问题:我如何重构HttpServerMainmain方法和HttpServerModule在这样的它们描述的设置过程变得可用e到我的GuiceServletConfigGuiceServletConfig看起来像这个工作?

回答

1

我从来没有像罐子一样的战争,所以我总是去寻找两种解决方案之一。但是,在使用战争时,不难设置可在IDE中工作的嵌入式Jetty服务器。为此,您可以使用WebAppContext使用web.xml进行设置。例如,请参阅this documentation。从那里,所有的东西都应该像Guice网站所倡导的那样工作。

但是,这不会产生可运行的战争(如java -jar yourapp.war),因为jar具有不同的内部布局。但是,如果你想要,你可以使用jetty-runner来解决这个问题,使用java -jar jetty-runner.jar yourapp.war

0

我把@Alexadvice,而忘记了创建一个独立的可运行war,而是着力打造只是war,做大部分的接线,在一个吉斯ServletModule

为此,我改变了我的HttpServerModule来扩展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现在可以被删除,因为它不再有用。