play框架依赖注入
所属分类 play
浏览量 643
https://www.playframework.com/documentation/2.8.x/ScalaDependencyInjection
Play supports both runtime dependency injection based on JSR 330 (described in this page) and compile time dependency injection in Scala
运行时注入 编译期注入
Runtime dependency injection is so called because the dependency graph is created, wired and validated at runtime.
If a dependency cannot be found for a particular component, you won’t get an error until you run your application.
https://www.playframework.com/documentation/2.8.x/ScalaCompileTimeDependencyInjection
Dependency injection achieves several goals:
1. It allows you to easily bind different implementations for the same component.
This is useful especially for testing, where you can manually instantiate components using mock dependencies or inject an alternate implementation.
2. It allows you to avoid global static state.
While static factories can achieve the first goal, you have to be careful to make sure your state is set up properly.
In particular Play’s (now deprecated) static APIs require a running application, which makes testing less flexible.
And having more than one instance available at a time makes it possible to run tests in parallel.
绑定组件的不同实现
测试时 手工初始化组件 使用mock组件 或者 替代的实现
避免全局的静态状态
静态工厂可以实现第一个目标 必须小心的维护状态
Play provides a number of built-in components and declares them in modules such as its BuiltinModule
play.api.Application
a router generated by the routes compiler
play.api.Mode
Dev, Test, Prod
Application creation is handled by the framework engine.
If you need to create an ad-hoc application, for example in case of unit testing, you can easily achieve this using:
val application = new DefaultApplication(new File("."), this.getClass.getClassloader, None, Play.Mode.Dev)
play.api.inject.guice.GuiceApplicationLoader
Play Guice module
com.typesafe.play.play-guice_2.12-2.8.8.jar
The Play team maintains the Guice module, which provides a GuiceApplicationLoader.
That does the binding conversion for Guice, creates the Guice injector with those bindings,
and requests an Application instance from the injector.
Play provides a BuiltInComponents trait that allows you to create a pure Scala implementation that wires together your app at compile time.
Play提供了一个BuiltInComponents特性,允许创建一个纯Scala实现,在编译时将应用连接在一起
The @Inject annotation can be used on fields or on constructors.
We recommend that you use it on constructors, for example:
import javax.inject._
import play.api.libs.ws._
class MyComponent @Inject() (ws: WSClient) {
// ...
}
constructor injection is generally the most clear, concise, and testable in Scala
构造函数注入通常是Scala中最清晰、最简洁、最可测试的
Guice is able to automatically instantiate any class with an @Inject on its constructor without having to explicitly bind it.
This feature is called just in time bindings
定义在Module中的绑定称为 明确声明绑定 Explicit bindings
当某个类型没有明确定义绑定时 ,Injector 试图构造 即时绑定(Just-in-time Bindings)
隐含绑定 implicit bindings
上一篇
下一篇
play action 要点
play HTTP routing
play框架日志
play 数据库访问
依赖注入 guice 实例
scala 多行字符串