Velocity & Google guice(1)

com.google.inject.spi.TypeListener を使うことにして、Velocity の利用方法を見直すことにした。
まずは、org.apache.velocity.Template の生成プロバイダであるが、
再利用を考えてpublic で 非 final にする、特別にアノテーションを用意する。

import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
 * org.apache.velocity.Template作成プロバイダ.
 * AbstractModule.bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)
 * による VelocityEngine が使用する属性 Properties を同時にインジェクトされるように定義されることが
 * 前提である。
 * Properties変数で、@VelocityProperty が付いてるものを VelocityEngine が使用する属性が
 * インジェクトされるように bindListener 定義をおこなう。
 * → VelocityTemplateModule 
 */

public class VelocityProvider implements Provider<Template>{
   @Inject @VelocityProperty private Properties prop;

   private String vmname;
   /**
    * @param vmname テンプレートファイル名
    */

   public VelocityProvider(String vmname){
      this.vmname = vmname;
   }
   /*
    * @see com.google.inject.Provider#get()
    */

   public Template get(){
      VelocityEngine velocityEngine = new VelocityEngine();
      try{
      velocityEngine.init(this.prop);
      return velocityEngine.getTemplate(this.vmname);

      }catch(Exception e){
         throw new RuntimeException(e);
      }
   }
}
--------------------------------------------------------------------
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface VelocityProperty{
}