scala-trait实现AOP编程
步骤1:声明表示基本动作方法的模块Taction
//声明表示基本动作方法的模块Taction
trait TAction {
def doAction
}
步骤2:定义一下加入了前置处理和后置处理的特征TBeforeAfter
trait TBeforeAfter extends TAction {
abstract override def doAction {
//doAction的前置处理
println("/entry before-action")
super.doAction
//doAction的后置处理
println("/exit after-action")
}
}
通过上面的abstract override def doAction {}语句来覆盖虚方法。具体来说这当中的super.doAction是关键,他调用了TAction的doAction方法。其原理是,由于doAction是虚方法,所以实际被执行的是被调用的实体类中所定义的方法。、
步骤3:实现实际执行的实体类RealAction
class RealAction extends TAction {
def doAction = {
println("** real action done!! **")
}
}
测试
object TestTrait {
def main(args : Array[String]) : Unit = {
val act1 = new RealAction with TBeforeAfter
act1.doAction
val act2 = new RealAction with TBeforeAfter with TTwiceAction
act2.doAction
val act3 = new RealAction with TTwiceAction with TBeforeAfter
act3.doAction
}
}
/entry before-action
** real action done!! **
/exit after-action
接着为他定义一下别的方面,然后将这些方面加入到同一对象的方法中。接着定义一个将源方法执行两遍的方面。
trait TTwiceAction extends TAction {
abstract override def doAction {
for (i <- 1 to 2){
super.doAction
println("==>NO. " + i)
}
}
}
下面,将TBeforeAfter和TtwiceAction混合在一起后执行一下。
/entry before-action
** real action done!! **
/exit after-action
==>NO. 1
/entry before-action
** real action done!! **
/exit after-action
==>NO. 2
伴随着原来方法的before/after动作一起各自执行了两次。接着将混入顺序颠倒后再试一下。
/entry before-action
** real action done!! **
==>NO. 1
** real action done!! **
==>NO. 2
/exit after-action
这样执行后,原来的实现方法被循环执行了两次,但是before/after则在循环以外整体只执行了一次。这是根据with语句定义的顺序来执行的,知道了这原理之后也就没有什么奇怪的了。Scala特性的如此混入顺序是和AspectJ的方面以及Spring的interceptor相同的。
scala-trait实现AOP编程的更多相关文章
- php-parser在Aop编程中的使用
在laravel下使用php-parser实现aop composer require nikic/php-parser Test.php <?php /** * Created by PhpS ...
- 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- 使用spring方式来实现aop编程
1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- 聊Javascript中的AOP编程
Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...
- 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--AOP编程
AOP编程在目前来说好像是大家都比较喜欢的.ASP.NET MVC中的Filter就是使用AOP实现的配置器模式.AOP在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...
- Scala Trait
Scala Trait 大多数的时候,Scala中的trait有点类似于Java中的interface.正如同java中的class可以implement多个interface,scala中的cals ...
- Scala 中的函数式编程基础(一)
主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...
- struts2.1笔记03:AOP编程和拦截器概念的简介
1.AOP编程 AOP编程,也叫面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用A ...
- Method Swizzling以及AOP编程:在运行时进行代码注入-备用
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
随机推荐
- Immediately-Invoked Puzzler
The Poplar Puzzle-makers weren’t too impressed. They barely noticed your simple and beautiful array ...
- MySQL监控、性能分析——工具篇
https://blog.csdn.net/leamonjxl/article/details/6431444 MySQL越来越被更多企业接受,随着企业发展,MySQL存储数据日益膨胀,MySQL的性 ...
- Windows改桌面文件路径
默认的桌面和用户文件都是C盘,每次重装系统要备份,为了方便可以把它设置到其他盘符,一种方式是通过一些软件功能,如360有一个C盘搬家,也可以修改注册表文件: Windows Registry Edit ...
- Oracle Data Guard 重要配置参数
Oracle Data Guard主要是通过为生产数据库提供一个或多个备用数据库(是产生数据库的一个副本),以保证在主库不可用或异常时数据不丢失并通过备用数据库继续提供服务.对于Oracle DG的配 ...
- Mac Oracle SqlDeveloper 快捷输入
用惯了 plsql 的快捷输入,换了 Mac Oracle SqlDeveloper反倒是找不到了,翻出去找了几次终于找到 SqlDeveloper -- preferences -- 数据库(dat ...
- 【CMS】安装CMS
Apache和MySQL准备好后开始安装CMS 在浏览器输入:http://localhost/install/index.php即可开始安装.
- Codeforces B - Berland National Library
B. Berland National Library time limit per test 1 second memory limit per test 256 megabytes input s ...
- or1200中载入存储类指令说明
下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 OR1200中实现的载入存储类指令有8条,每条指令的作用与说明如表9.1所看到的. watermark/2/text/aHR0cDo ...
- Protobuf学习 - 入门(转)
从公司的项目源码中看到了这个东西,觉得挺好用的,写篇博客做下小总结.下面的操作以C++为编程语言,protoc的版本为libprotoc 3.2.0. 一.Protobuf? 1. 是什么? Goo ...
- cannot use 'throw' with exceptions disabled(转)
在为 DragonBonesCPP/refactoring 的 cocos2d-x-3.2 demo 增加 Android 编译时,NDK 报了一个编译错误: error: cannot use ‘t ...