日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Lift当中的AnyVarTrati源码解析

發布時間:2025/4/16 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lift当中的AnyVarTrati源码解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Lift2.0當中,AnyVarTrait是一個非常重要的trait,很多重要的像RequestVar,SessionVar都是繼續該接口的,

首先看下大致接口代碼

abstract class AnyVar[T, MyType <: AnyVar[T, MyType]](dflt: => T) extends AnyVarTrait[T, MyType] {self: MyType =>protected def calcDefaultValue: T = dflt }/*** Abstract a request or a session scoped variable.*/ trait AnyVarTrait[T, MyType <: AnyVarTrait[T, MyType]] extends PSettableValueHolder[T] with HasCalcDefaultValue[T] { AnyVarTrait繼承了PSettableValueHolder,它的接口是: trait ValueHolder {type ValueTypedef is: ValueTypedef get: ValueType }trait SettableValueHolder extends ValueHolder {def set(in: ValueType): ValueType }trait PValueHolder[T] extends ValueHolder {type ValueType = T// def manifest: Manifest[T] }trait PSettableValueHolder[T] extends PValueHolder[T] with SettableValueHolder} }

?

PSettableValueHolder主要是實現了類似于get,set的這樣的方法,比較簡單,寫一個比較簡單的實現PSettableValueHolder的例子:

class Exampletrait ValueHolder{type ValueTypedef is: ValueTypedef get: ValueType }trait SettableValueHolder extends ValueHolder{def set(in: ValueType): ValueType }trait PValueHolder[T] extends ValueHolder{type ValueType = T }trait PSettableValueHolder[T] extends PValueHolder[T] with SettableValueHolderclass TestPSettableValueHolder[T] extends PSettableValueHolder[T] {protected var testVal: T=_def set(in: T):T ={testVal=inin}def get:T={testVal}def is:T={testVal} }object Test{def main(args: Array[String]){val test=new TestPSettableValueHolder[String]test.set("Test22")println(test.get)} }

需要重載的方法:

protected lazy val name = VarConstants.varPrefix+getClass.getName+"_"+__nameSaltprotected def findFunc(name: String): Box[T]protected def setFunc(name: String, value: T): Unitprotected def clearFunc(name: String): Unitprotected def wasInitialized(name: String): Booleanprotected def calcDefaultValue: T/*** A non-side-effecting test if the value was initialized*/protected def testWasSet(name: String): Booleanprotected def __nameSalt = ""type CleanUpParam/*** Different Vars require different mechanisms for synchronization. This method implements* the Var specific synchronization mechanism*/def doSync[F](f: => F): F

?

實現了PSettableValueHolder[T] trait的方法:

?

/*** The current value of the variable*/def is: T = doSync {findFunc(name) match {case Full(v) => vcase _ => val ret = calcDefaultValuetestInitializedapply(ret)// Use findFunc so that we clear the "unread" flagfindFunc(name) match {case Full(v) => vcase _ => ret}}}private def testInitialized: Unit = doSync {if (!wasInitialized(name)) {registerCleanupFunc(_onShutdown _)}}/*** Shadow of the 'is' method*/def get: T = is/*** Shadow of the apply method*/def set(what: T): T = apply(what)

該trait的apply實現:

/*** Set the session variable** @param what -- the value to set the session variable to*/def apply(what: T): T = {testInitializedsetFunc(name, what)what}

?

最后一個該trait非常重要的方法 :在該var的生命周期內去修改它的Value:

/*** Change the value of the Var for the lifespan of the function*/def doWith[F](newVal: T)(f: => F): F = {val old = findFunc(name)setFunc(name, newVal)try {f} finally {old match {case Full(t) => setFunc(name, t)case _ => clearFunc(name)}}}

再來看下該trait的具體實現:SessionVar和RequestVar

SessionVar,看下他的幾個主要實現AnyVarTrait的實現:

override protected def findFunc(name: String): Box[T] = S.session match {case Full(s) => s.get(name)case _ =>if (showWarningWhenAccessedOutOfSessionScope_?)logger.warn("Getting a SessionVar "+name+" outside session scope") // added warning per issue 188Empty}override protected def setFunc(name: String, value: T): Unit = S.session match {case Full(s) => s.set(name, value)case _ =>if (showWarningWhenAccessedOutOfSessionScope_?)logger.warn("Setting a SessionVar "+name+" to "+value+" outside session scope") // added warning per issue 188}/*** Different Vars require different mechanisms for synchronization. This method implements* the Var specific synchronization mechanism*/def doSync[F](f: => F): F = S.session match {case Full(s) =>// lock the session while the Var-specific lock object is found/createdval lockName = name + VarConstants.lockSuffixval lockObj = s.synchronized {s.get[AnyRef](lockName) match {case Full(lock) => lockcase _ => val lock = new AnyRefs.set(lockName, lock)lock}}// execute the query in the scope of the lock objlockObj.synchronized {f}case _ => f}

RequestVar的實現:

override protected def findFunc(name: String): Box[T] = RequestVarHandler.get(name)override protected def setFunc(name: String, value: T): Unit = RequestVarHandler.set(name, this, value)override protected def clearFunc(name: String): Unit = RequestVarHandler.clear(name)override protected def wasInitialized(name: String): Boolean = {val bn = name + VarConstants.initedSuffixval old: Boolean = RequestVarHandler.get(bn) openOr falseRequestVarHandler.set(bn, this, true)old}/*** Different Vars require different mechanisms for synchronization. This method implements* the Var specific synchronization mechanism*/def doSync[F](f: => F): F = f // no sync necessary for RequestVars... always on the same thread

轉載于:https://www.cnblogs.com/zoujiaxue/archive/2010/08/24/1807305.html

總結

以上是生活随笔為你收集整理的Lift当中的AnyVarTrati源码解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。