scalatra之隐式request

scalatra隐式request

在之前scala之隐式servletContext这篇文章中,我们提到了scalatra实现的隐式servletContext是如何实现的,那今天我们来看看在scalatra中是如何利用scala的特性来实现隐式HttpServletRequest的:

ScalatraBase

在ScalatraBase这个伴生对象中,我们可以看到如下代码:

1
2
3
4
5
6
7
8
private[this] val KeyPrefix: String = classOf[FutureSupport].getName
val Callbacks: String = s"$KeyPrefix.callbacks"

def addCallback(callback: Try[Any] => Unit)(implicit request: HttpServletRequest): Unit = {
//http://johnwonder.github.io/2017/03/25/scala-implicit1/
//放入AttributesMap中
request(Callbacks) = callback :: callbacks
}

ServletApiImplicits

这边的request(Callbacks)到底是啥呢?我们来结合scala的隐式调用来分析

ScalatraBase中引入了ServletApiImplicits

1
import org.scalatra.servlet.ServletApiImplicits._
1
2
3
4
5
6
//隐式的ServletApi
trait ServletApiImplicits {

implicit def enrichRequest(request: HttpServletRequest): RichRequest =
RichRequest(request)
}

那说明实现是在RichRequest中

RichRequest

1
2
case class RichRequest(r: HttpServletRequest) extends AttributesMap {
}

我们看到它还实现了AttributesMap这个trait

AttributesMap

1
2
3
4
5
6
7
8
9

/**
*映射ServletRequest的属性到map
* Adapts attributes from servlet objects (e.g., ServletRequest, HttpSession,
* ServletContext) to a mutable map.
*/

trait AttributesMap extends Map[String, Any] with MutableMapWithIndifferentAccess[Any] {

}

我们看到他最终是继承自Map的,所以request()是Map调用。

欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%