0

これは、サーブレットを接続するためのコードから、jettyを埋め込むためのコードをより適切に分離することに関する質問です。

実行可能な war、つまり、既存の Jetty コンテナーにドロップできる war ファイル、または のようなコマンドを使用してスタンドアロンで実行できるように、このサンプル コードを適応させようとしていますjava -jar webapp-runnable.war。サンプル コードは、次の 2 つのブログ投稿に属しています: 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 のこのレポから始めました。

1) AbstractModule 型の匿名内部クラスを抽出し、com.teamlazerbeez.http.HttpServerMainそれを新しい class に入れましたcom.teamlazerbeez.http.HttpServerModule。このクラスは、(l36) で Guice Injector を作成するときにインスタンス化されるようになりましHttpServerMain

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