Guice:获取依赖里面提供的方法
Guice: Getting Dependencies inside provides method
我正在编写一个 Guice 模块,在那个模块中我有一个 provides 方法。
class FooModule extends ScalaModule {
@Provides
@Singleton
def providesFoo() : Foo = {
new Foo()
}
}
问题是 new Foo
的构造函数将 Type Bar 作为参数。
我想知道如何让 guice 给我一个 Bar 实例,以便我可以在模块中对 Foo 做一个新操作
通过将这些参数指定为方法参数来请求这些参数。来自 Provides method wiki documentation:
Dependencies can be passed in as parameters to the method. The injector will exercise the bindings for each of these before invoking the method.
所以:
@Provides
@Singleton
def providesFoo(val bar : Bar) : Foo = {
new Foo(bar)
}
我正在编写一个 Guice 模块,在那个模块中我有一个 provides 方法。
class FooModule extends ScalaModule {
@Provides
@Singleton
def providesFoo() : Foo = {
new Foo()
}
}
问题是 new Foo
的构造函数将 Type Bar 作为参数。
我想知道如何让 guice 给我一个 Bar 实例,以便我可以在模块中对 Foo 做一个新操作
通过将这些参数指定为方法参数来请求这些参数。来自 Provides method wiki documentation:
Dependencies can be passed in as parameters to the method. The injector will exercise the bindings for each of these before invoking the method.
所以:
@Provides
@Singleton
def providesFoo(val bar : Bar) : Foo = {
new Foo(bar)
}