Google Guice
前回作成したTestRunnerに対して、Moduleのクラス名のsafixにTestが付いているものを優先して読み込むように改造した。 #テスト時は、xxxxを利用したいといった場合用です。 public class GuiceJunitRunner extends BlockJUnit4ClassRunner { protected fina…
GuiceでServeletを利用するには、guice-servlet.jarが必要になります。 mavenのglobal repositoryにないので、ローカルリポジトリに登録します。 %maven install:install-file -Dfile=guice-servlet-3.0.jar -DgoupdId=com.google.guice -DartifactId=guice-…
factory系で生成したオブジェクトをDIしましょうという機能らしい。 やり方は2通りで、Providerを実装するか、@Providesインスタンスで指定するか。ただのhogeを返すProvider. public class SampleProvider implements Provider<String> { public String get() { ret</string>…
結論は、bindInterceptor()に登録した順にInterceptorが起動する(っぽい)。Interceptor public class Sample1Intercetpor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.pr…
DIの発想にたつと、プロパティの取得をstaticにAbstractModuleに書くのはよくない。 なので、Injectして与えるということになるが・・・。 なんか、複雑やね。 XADatasouceModuleを見ただけでは、生成の仕方が不明で、Conventionもみないといけないし・・・…
Propertiesの内容をInjectしてインスタンスを生成するファクトリです。 Injectorは、On-Demandとして利用します。 public class GuiceInstanceFactory { public static <T>T getInstance(Class<T> cls, final Properties properties) { Injector injector = Guice.</t></t>…
Google GuiceでDIコンテナにあれば登録としたい場合は、次の通り。 @Inject(option=true) String hoge;
Google Guieceにて、SLF4JのCustom Injectorを作成する。 #Google Guice wikiにあったのをコピーしただけ・・pom.xmlにSLFJ4+logbackを追加する。 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>[1.,)</version> <scope>runtime</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> </dependency>
Google Guiceを利用したJUnit試験用のRunnerとAnnotationのサンプル実装です。Runner public class GuiceJunitRunner extends BlockJUnit4ClassRunner { protected final static Log _log = LogFactory.getLog(GuiceJunitRunner.class); public GuiceJunitRu…