Scala play框架中@Singleton的使用
Use of @Singleton in Scala play framework
在定义 Scala 控制器时,使用 @Singleton 注释将 class 标记为单例:
@Singleton
class Application
https://docs.oracle.com/javaee/7/api/javax/inject/Singleton.html 将单例定义为 'Identifies a type that the injector only instantiates once. Not inherited.' 那么 Scala 播放依赖注入框架是否依赖于 Java 依赖注入?
来自 https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection 'Play supports both runtime dependency injection based on JSR 330 (described in this page) and compile time dependency injection in Scala.' 正在使用 @Singleton 利用 'dependency injection based on JSR 330' 所以要使用 'compile time dependency injection in Scala' 需要什么?
编译时和 运行time DI 之间的主要区别在于,对于 运行time DI,在 运行 您的应用程序之前,您不会知道您的依赖项是否会被连接和实现(即你会得到一个 运行 时间错误而不是编译时间错误)
有关 play 的编译时 DI 支持的更多信息,我强烈建议您查看有关它的 official documentation。
is Scala play dependency injection framework relying on Java
dependency injection ?
是的,所以你需要写import javax.inject._
你使用DI的每个文件。
基本上你需要做的就是
・ 定义接口为trait
trait FooService {
def getBar(baz: String):Future[Bar]
}
・实现接口
class FooServiceImpl extends FooService {
def getBar(baz: String) = ???
}
・ 通过Module.scala(guice 样式)
绑定它们
class Module extends AbstractModule {
override def configure() = {
bind(classOf[FooService]).to(classOf[FooServiceImpl])
}
}
・使用它
class FooController @Inject()(fooService: FooService)(implicit exec: ExecutionContext) extends Controller {
def index = Action.async = {
fooService.getBar("fooBar").map{_.doWhatEverYouWant}
.....
}
}
如您所见,使用这种方式时需要定义class参数DI.This是您不能使用Scala object
和使用[=17=的原因] 相反。
在定义 Scala 控制器时,使用 @Singleton 注释将 class 标记为单例:
@Singleton
class Application
https://docs.oracle.com/javaee/7/api/javax/inject/Singleton.html 将单例定义为 'Identifies a type that the injector only instantiates once. Not inherited.' 那么 Scala 播放依赖注入框架是否依赖于 Java 依赖注入?
来自 https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection 'Play supports both runtime dependency injection based on JSR 330 (described in this page) and compile time dependency injection in Scala.' 正在使用 @Singleton 利用 'dependency injection based on JSR 330' 所以要使用 'compile time dependency injection in Scala' 需要什么?
编译时和 运行time DI 之间的主要区别在于,对于 运行time DI,在 运行 您的应用程序之前,您不会知道您的依赖项是否会被连接和实现(即你会得到一个 运行 时间错误而不是编译时间错误)
有关 play 的编译时 DI 支持的更多信息,我强烈建议您查看有关它的 official documentation。
is Scala play dependency injection framework relying on Java dependency injection ?
是的,所以你需要写import javax.inject._
你使用DI的每个文件。
基本上你需要做的就是
・ 定义接口为trait
trait FooService {
def getBar(baz: String):Future[Bar]
}
・实现接口
class FooServiceImpl extends FooService {
def getBar(baz: String) = ???
}
・ 通过Module.scala(guice 样式)
绑定它们class Module extends AbstractModule {
override def configure() = {
bind(classOf[FooService]).to(classOf[FooServiceImpl])
}
}
・使用它
class FooController @Inject()(fooService: FooService)(implicit exec: ExecutionContext) extends Controller {
def index = Action.async = {
fooService.getBar("fooBar").map{_.doWhatEverYouWant}
.....
}
}
如您所见,使用这种方式时需要定义class参数DI.This是您不能使用Scala object
和使用[=17=的原因] 相反。