Locked History Actions

Diff for "guice/Manual/Integration/WebandServlets/ServletModule"

Differences between revisions 2 and 3
Deletions are marked like this. Additions are marked like this.
Line 71: Line 71:
== 可能な注入 == == 提供される注入 ==

サーブレットモジュールのインストール

GuiceFilterを設定して起動すればGuiceサーブレットがセットアップされるが、 しかしGuiceサーブレットを実際に使うためにはServletModuleインスタンスのインストールが必要である。

   Guice.createInjector(new ServletModule());

このモジュールはリクエスト及びセッションスコープをセットアップし、フィルターとサーブレットをコンフィギュレーションするための方法を提供する。 インジェクタを作成する場所はいかなるところでも構わないのだが、論理的な場所としてServletContextListenerがある。

ServletContextListenerはウェブアプリケーションが配備された直後、リクエストが到着する以前にトリガーされるJavaサーブレットである。 Guiceサーブレットはサブクラス化するための便利なユーティリティを提供しているの、あなた自身のServletContextListenerを作成することができる。

public class MyGuiceServletConfig extends GuiceServletContextListener {

  @Override
  protected Injector getInjector() {
    return Guice.createInjector(new ServletModule());
  }
}

次に、以下のようなweb.xmlを追加し、サーブレットコンテナがアプリケーション配備時にこのクラスをトリガーできるようにする。

<listener>
  <listener-class>com.example.MyGuiceServletConfig</listener-class>
</listener>

これで、必要に応じてGuiceサーブレットを使えるようになった。 ただし、必ずしもGuiceサーブっトを使うために、ServletContextListenerは必要ではない、 インジェクタ生成時にServletModuleのインストールを忘れさえしなければ。

バインディング言語

ServletModuleをweb.xml配備デスクリプタのコードによる置き換えと考えてみよう。 フィルタとサーブレットは通常のJavaメソッド呼び出しによってコンフィギュレーションされる。 以下はGuiceインジェクタを作成する際にサーブレットを登録する典型的な例である。

   Guice.createInjector(..., new ServletModule() {

     @Override
     protected void configureServlets() {
       serve("*.html").with(MyServlet.class)
     }
   }

これは.htmlで終了するすべてのウェブリクエストをサービスするためのMyServletという名のサーブレット(HttpServletのサブクラス)を登録するものである。

       serve("/my/*").with(MyServlet.class)

フィルタマッピング

サーブレットフィルタを同様のシンタックスでマップすることができる。

      filter("/*").through(MyFilter.class);

これはMyFilterを通して到着するすべてのリクエストをルーティングし、 他のマッチするフィルタを通し、最終的に処理サーブレットに配布する。

注意:すべてのサーブレット(あるいはフィルタ)は@Singletonでなければならない。 もしこれらのクラスに直鉄的にアノテーションできない場合は、bind(..).in(Singleton.class)でバインドすること、 separate to the filter() or servlet() rules. 他のスコープへのマッピングはエラーとなる。 これはサーブレット仕様の一貫性保持のためである。 Guiceサーブレットは古いSingleThreadModelはサポートしていない。

提供される注入

サーブレットモジュールをインストールすることにより、サーブレットフレームワークから様々なクラスにアクセスが可能となる。 ServletModuleをインストールすると、サーブレットプログラミングモデルで便利なものが得られるし、デフォルトでGuiceが注入するオブジェクトが得られる。

{{{ @RequestScoped class SomeNonServletPojo {

} }}}

The request and response are scoped to the current http request. Similarly the http session object is scoped to the current user session. In addition to this you may also inject the current ServletContext and a map of the request parameters using the binding annotation @RequestParameters as follows:

@Inject @RequestParameters Map<String, String[]> params;

This must be a map of strings to arrays of strings because http allows multiple values to be bound to the same request parameter, though typically there is only one. Note that if you want access to any of the request or session scoped classes from singletons or other wider-scoped instances, you should inject a Provider<T> instead. Dispatch Order

You are free to register as many servlets and filters as you like this way. They will be compared and dispatched in the order in which the rules appear in your ServletModule:

   Guice.createInjector(..., new ServletModule() {

     @Override
     protected void configureServlets() {
       filter("/*").through(MyFilter.class);
       filter("*.css").through(MyCssFilter.class);
       // etc..

       serve("*.html").with(MyServlet.class);
       serve("/my/*").with(MyServlet.class);
       // etc..
      }
    }

This will traverse down the list of rules in lexical order. For example, a url /my/file.js will first be compared against the servlet mapping:

       serve("*.html").with(MyServlet.class);

And failing that, it will descend to the next servlet mapping:

       serve("/my/*").with(MyServlet.class);

Since this rule matches, Guice Servlet will dispatch the request to MyServlet and stop the matching process. The same process is used when deciding the dispatch order of filters (that is, matched to their order of appearance in the binding list). Varargs Mapping

The two mapping rules above can also be written in more compact form using varargs syntax:

       serve("*.html", "/my/*").with(MyServlet.class);

This way you can map several URI patterns to the same servlet. A similar syntax is also available for filter mappings.