相关疑难解决方法(0)

使用Guice将参数传递给构造函数

我有一个工厂如下,

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构造函数?

有任何想法吗?

java dependency-injection guice

32
推荐指数
3
解决办法
4万
查看次数

Guice injectMembers方法

我理解使用构造函数注入优于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)

guice

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

guice ×2

dependency-injection ×1

java ×1