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源码解析相关推荐
- Framework 源码解析知识梳理(5) startService 源码分析
一.前言 最近在看关于插件化的知识,遇到了如何实现Service插件化的问题,因此,先学习一下Service内部的实现原理,这里面会涉及到应用进程和ActivityManagerService的通信, ...
- 2015.07.20MapReducer源码解析(笔记)
MapReducer源码解析(笔记) 第一步,读取数据源,将每一行内容解析成一个个键值对,每个键值对供map函数定义一次,数据源由FileInputFormat:指定的,程序就能从地址读取记录,读取的 ...
- clickhouse原理解析与开发实战 pdf_重识SSM,“超高频面试点+源码解析+实战PDF”,一次性干掉全拿走...
重识SSM,"超高频面试点"+"源码解析"+"实战PDF",一次性干掉全拿走!! 01 超高频面试点知识篇 1.1 Spring超高频面试点 ...
- Mybatis运行原理及源码解析
Mybatis源码解析 一.前言 本文旨在mybatis源码解析,将整个mybatis运行原理讲解清楚,本文代码地址: https://github.com/lchpersonal/mybatis-l ...
- Spring Session源码解析
AbstractHttpSessionApplicationInitializer,很明显它是一个初始化的类,它是一个抽象类,可以理解为一个公用的基类,然后看一下onStartup这个方法,最主要的方 ...
- Java生鲜电商平台-秒杀系统微服务架构设计与源码解析实战
Java生鲜电商平台-秒杀系统微服务架构设计与源码解析实战 Java生鲜电商平台- 什么是秒杀 通俗一点讲就是网络商家为促销等目的组织的网上限时抢购活动 比如说京东秒杀,就是一种定时定量秒杀,在规定 ...
- Android Glide图片加载框架(二)源码解析之into()
文章目录 一.前言 二.源码解析 1.into(ImageView) 2.GlideContext.buildImageViewTarget() 3.RequestBuilder.into(Targe ...
- Android Glide图片加载框架(二)源码解析之load()
文章目录 一.前言 二.源码分析 1.load() Android Glide图片加载框架系列文章 Android Glide图片加载框架(一)基本用法 Android Glide图片加载框架(二)源 ...
- Android Glide图片加载框架(二)源码解析之with()
文章目录 一.前言 二.如何阅读源码 三.源码解析 1.with() Android Glide图片加载框架系列文章 Android Glide图片加载框架(一)基本用法 Android Glide图 ...
最新文章
- C#中使用的yield关键字是什么?
- java 详解 搭建 框架_在Eclipse中搭建Struts框架过程详解
- hdu 3062 Party(2-sat,3级)
- Jsp对字符串的处理
- Bootstrap3.0学习第八轮(工具Class)
- 怎样Interlocked.Increment一个反射得到的field?
- php无法清除缓冲区怎么解决,php – 无法删除缓冲区 没有缓冲区要删除
- C++ string获取字符串元素:[]和at()
- 2018年AI智商将达到多少?未来智能实验室启动第三次世界AI智商评测
- CV LRO mission (LROC introduction)
- 方维分享增加列表页图片alt title属性,提高转化率
- 光猫的ip地址段和路由器ip地址段互换
- 【历史上的今天】2 月 7 日:“电脑大王”王安诞生;发明“上网冲浪”的程序员出生;2013 年图灵奖得主生日
- “Unexpected end of JSON input while parsing near···”错误解决方案
- keras UserWarning: Discrepancy between trainable weights and collected trainable weigh...
- 南朝皇帝简介-20170604
- 【机器学习】五种超参数优化技巧
- python从入门到入土表情包-Python自动生成表情包,python在手,从此斗图无敌手
- MATLAB学习七(二):数组比较sortrows
- 说说Oracle分区
热门文章
- rsync文件实时同步_从文件同步rsync算法谈起
- python编程高手教程_写给编程高手的Python教程(11) 深入类和对象
- JDK注解(内置和自定义)
- HTML 怎么修改,怎么修改HTML
- 的工作空间查看_【工作推荐】裱花师、现金会计、汽车维修等热门岗位汇总!...
- Java数据结构与算法:二叉树
- python002 一 eg: Python 入门技巧__环境搭建__git使用、git本地操作、 自建gitlab服务器
- php 自动创建文件,php:自动创建多级文件夹
- python中utf-8是什么意思_python中Unicode和UTF-8的区别
- android mvvm livedata_再谈Android应用架构——Jetpack VS 生命周期