我正在使用 Guice Assisted Inject库为我建立一个工厂。我目前的设置是这样的:
class MyObject {
@Inject public MyObject(@Assisted FirstDep first, @Assisted SecondDep second, ThirdDep third) { /**/ }
}
class FirstDep { /* nothing to see here */ }
class SecondDep {
@Inject public SecondDep(@Assisted FirstDep first) { /**/ }
}
class ThirdDep { /* nothing to see here either */ }
class MyModule extends AbstractModule {
@Override public void configure() {
bind(ThirdDep.class);
install(new FactoryModuleBuilder().build(MyObjectFactory.class));
}
}
interface MyObjectFactory {
SecondDep createSecond(@Assisted FirstDep first);
MyObject createMyObject(@Assisted FirstDep first, @Assisted SecondDep second);
}
这迫使我明确地创建一个SecondDep
using factory.createController(first, factory.createSecond(first))
。是否可以更改我的绑定,以便我可以简单地做factory.createController(first)
,它会自动使用SecondDep
绑定和我传入的参数?