我有一个工厂如下,
public final class Application {
private static IFoo foo;
public static IFoo getFoo(String bar)
{
// i need to inject bar to the constructor of Foo
// obvious i have to do something, not sure what
Injector injector = Guice.createInjector();
logger = injector.getInstance(Foo.class);
return logger;
}
}
Run Code Online (Sandbox Code Playgroud)
这是Foo的定义:
class Foo
{
Foo(String bar)
{
}
}
Run Code Online (Sandbox Code Playgroud)
好.我不知道如何使用Guice将此参数传递给Foo构造函数?
有任何想法吗?
我理解使用构造函数注入优于setter注入的好处,但在某些情况下,我必须坚持使用基于setter的注入.我的问题是如何使用injector.injectMembers()方法注入所有基于setter的注入类的成员?
//I am calling this method in init method of my application
private static final Injector injector = Guice.createInjector(new A(), new B());
//Injecting dependencies using setters of all classes bound in modules A and B
injector.injectAllMembers()??
Run Code Online (Sandbox Code Playgroud)