Guice によって注入された Factory の使用に問題があります。
この素晴らしい記事http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/assistedinject/FactoryModuleBuilder.htmlを読みましたが、まだ何かを理解していません。つまり、モジュールが使用されないのはなぜですか? Guice.createInjector()メソッドのように。
この単純なアプリケーションを試してみたところ、必要な Factory を Guice が解決できなかったため、NullPointerExceptionが発生しました。
public interface FooInterface
{
String getString();
}
public class Foo implements FooInterface {
String bar;
@Inject
Foo(@Assisted String bar)
{
Log.i("main", "Foo constructor");
this.bar = bar;
}
public String getString(){
Log.i("main", "getString");
return this.bar;
}
}
public interface FooFactory
{
FooInterface create(String bar);
}
ここに構成モジュールがあります
public class ConfigurationModule extends AbstractModule
{
@Override
protected void configure()
{
Log.i("main", "Configuration module");
install(new FactoryModuleBuilder().implement(FooInterface.class, Foo.class).build(FooFactory.class));
}
}
そして私の活動
public class MyActivity extends Activity implements View.OnClickListener
{
@Inject private FooFactory fooFactory;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("main", "onCreate");
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
Log.i("main", "onClick");
FooInterface foo = this.fooFactory.create("foo name");
String str = foo.getString();
Toast.makeText(getApplicationContext(), "String is: "+ str, Toast.LENGTH_SHORT);
}
}
ログからわかるように、Fooコンストラクターは呼び出されません。ConfigurationModuleと同じです。このモジュールがどこで使用されているかわかりません。何か案は?多分私は何かを逃していますか?