我刚开始使用Guice,所以我的理解可能完全错误。我可以在使用Guice的方法中获得实例吗?
我有一个接口Foo
和implmentation FooImp
我用下面的代码我的模块中注入它:
bind(Foo.class).annotatedWith("Foo").toInstance(foo); // foo is an instance of FooImp
然后我现在想在我的代码只要一个Foo
实例与“Foo”的名称注释将被foo
取代?
@Named("Foo")
Foo fooInst; // fooInst will be foo at runtime?
如果我是正确的,我怎么能做到这一点:
public Foo doSomething() { // for some reason there must be no arguments
@Named("Foo")
Foo fooInst; // injecting here seems not allowed...?
fooInst.do();
}
清晰且有帮助的答案。感谢您的帮助! – NSF