Springサポート/Guiceサポート


T2の拡張モジュールとして、Guiceのサポートをコミットしました.
リリースは次のT2のバージョンにあわせてしようかとおもっています.


Guiceのアダプタで最初いろいろ機能つけようかと思ったけど、シンプルに
GuiceのModuleクラスをServiceLoader経由でとって、それをコンポーネントとして登録したInjectorから
getComponentして終わりにしちゃいました.


なので利用する側ではやるべきことは2つです.
1.プロジェクトのビルドパス配下に、META-INF/services/com.google.inject.Moduleというファイルを置いて、
その中にModuleの実装クラスのFQCNを書いておく。ちなみに改行して複数記述することも可能です.
2.Moduleの実装クラスで、インジェクト設定をGuice流儀で書く


いじょう。

イメージとしてはこんな感じです。

META-INF/services/com.google.inject.Moduleのなか.

org.t2framework.plugins.container.TargetModule


TargetModuleはこんな感じ.

package org.t2framework.plugins.container;

import com.google.inject.AbstractModule;

public class TargetModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(Hoge.class).to(HogeImpl.class);
	}

}


テストコード.

package org.t2framework.plugins.container;

import junit.framework.TestCase;

public class GuiceAdapterTest extends TestCase {

	public void test1() throws Exception {
		GuiceAdapter adapter = new GuiceAdapter();
		try {
			adapter.init();
			Hoge component = adapter.getComponent(Hoge.class);
			assertNotNull(component);
			assertNotNull(component.getMessage());

			component = adapter.getComponent(HogeImpl.class);
			assertNotNull(component);
			assertNotNull(component.getMessage());
		} finally {
			adapter.destroy();
		}
	}
}


ホントはGuice2でやろうとしたけど、なんか正式リリースはまだみたいなのでもう少し待ちます.


で毎度恒例のSNAPSHOTデプロイ.

Guice Adapter

ソースコード


ついでにSpringも.

Spring Adapter

ソースコード