Wicket+Google guice+iBATIS

Wicket で Google guice によるロジックをDI(インジェクト)で実行、
そして、iBATIS の SQLMap の処理も、Wicket 上で動作する Google guice のインジェクト
される結果により実行するパターンである。
このパターン処理の流れを定義するのに、XML等の設定ファイルが不要である。
あえて、XMLを書くのは、iBATIS の利用から、SQL文を書いたXML
DB接続定義だけであり、本当にXMLで書いた方が良い部分が残される!

wicket-guice-1.4.1.jar に入っている
org.apache.wicket.guice.GuiceComponentInjector
が素晴らしい。com.google.inject.Guice#createInjector と同様に
引数に、Module...m の複数指定を継承してる。

今回作ったパターン
-----------------------------------------------------------
public class FooApplication extends WebApplication{

   @Override
   protected void init(){
      // Guice インジェクション
      // com.google.inject.AbstractModule 継承の Module を用意してインジェクト定義登録
      // iBATIS を使用する場合の SqlMapClient のインジェクト定義は、SqlMapModule
      // PaperModule → Papaer という任意ロジックのインジェクト定義

      addComponentInstantiationListener(new GuiceComponentInjector(this,new Module[]{
            new SqlMapModule()
            ,new PaperModule()
      })
);


   }
   @Override
   public Class<? extends Page> getHomePage(){
      return HomePage.class;
   }
}
-----------------------------------------------------------
public class HomePage extends WebPage{
   private static final long serialVersionUID = 1L;
   // ↓インジェクトされるロジック
   @Inject Paper paper;

   public HomePage(final PageParameters parameters){

      final Label messageLabel = new Label("message","テスト"));
      final TextField<String> inputField
       = new TextField<String>("inputField",new Model<String>(""));

      Form<Void> submitForm = new Form<Void>("submitForm"){
         private static final long serialVersionUID = 1L;
         // submit ボタンで実行する onSubmit()
         @Override
         protected void onSubmit(){
            // インジェクトされたロジックの実行
            String color = HomePage.this.paper.getColor();
            String sysdate = HomePage.this.paper.getSysDate();
              :
            messageLabel.setEscapeModelStrings(false); 
            messageLabel.setDefaultModelObject(
               inputField.getDefaultModelObject()
                 +"  <b>color</b> = "+color
                 +"  <b>DB  SYSDATE</b> = "+sysdate
            );
         }
      };

      add(messageLabel);
      submitForm.add(inputField);

      add(submitForm);
   }

}
-----------------------------------------------------------
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

public class PaperModule extends AbstractModule{
   @Override
   protected void configure(){
      binder().bind(Paper.class).to(PaperImpl.class);

      binder().bind(String.class).annotatedWith(Names.named("COLOR"))
         .toInstance("Red");
      bindConstant().annotatedWith(Names.named("SIZE")).to(120);
   }
}
------------------------------------------------------------
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.ibatis.sqlmap.client.SqlMapClient;

public class PaperImpl implements Paper{
   @Inject @Named("COLOR") private String color;
   @Inject @Named("SIZE")  private int sizeInt;
   @Inject private SqlMapClient sqlmapclient;
   private SimpleDateFormat sfYMDHMS;

   public PaperImpl(){
      this.sfYMDHMS = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
   }
   @Override
   public String getSysDate(){

      String rtn;
      try{
      Date dt = (Date)this.sqlmapclient.queryForObject("getSysDate");
      rtn = this.sfYMDHMS.format(dt);
      }catch(SQLException e){
         rtn = "SQLException "+e.getMessage();
      }
      return rtn;
   }
   @Override
   public String getColor(){
      return this.color;
   }
   @Override
   public int size(){
      return this.sizeInt;
   }
}
-----------------------------------------------------------
import java.io.IOException;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
/**
 * SqlMapClient バインド定義
 * @Inject private SqlMapClient sqlmapclient;
 */

public class SqlMapModule extends AbstractModule{
   @Override
   protected void configure(){
   }
   @Provides
   protected SqlMapClient buildSqlMapClient() throws IOException{
      return SqlMapClientBuilder.buildSqlMapClient(
                Resources.getResourceAsReader("SqlMapConfig.xml"));
   }
} ------------------
必要なJARは、、

aopalliance.jar
cglib-2.2.jar
guice-2.0.jar
ibatis-2.3.4.726.jar
log4j-1.2.15.jar
mysql-connector-java-5.1.15-bin.jar
ojdbc14.jar
sl4j-api-1.5.8.jar
sl4j-log4j12-1.5.8.jar
wicket-1.4.1.jar
wicket-guice-1.4.1.jar
wicket-ioc-1.4.1.jar