Groovy探索之MOP 十 Interceptor 二
来源:互联网 发布:外贸原单淘宝店推荐 编辑:程序博客网 时间:2024/05/17 20:29
Groovy探索之MOP 十 Interceptor 二
在本系列的《Groovy探索之MOP 九 Interceptor 一》中,我们已经详细的介绍了一个简单的拦截器类的方方面面,使得我们初步有了拦截器的基础。本篇需要在前面的拦截器类的基础上,进一步用拦截器类来实现我们的AOP编程。
首先,我们在本系列的第一篇中,所拦截的方法都是固定的方法。现在,我们需要把它扩展成由拦截器类的使用者来指定被拦截的方法。
先还是给出需要被拦截的类来:
class Foo {
def test1()
{
println 'test1'
}
def test2()
{
println 'test2'
}
def test3()
{
println 'test3'
}
}
然后来给出我们的拦截器类:
class MethodsInterceptor implements Interceptor{
def methods
Object beforeInvoke(Object object, String methodName, Object[] arguments){
if( methodName in this.methods )
{
println "before invoking $methodName"
}
null
}
boolean doInvoke(){ true }
Object afterInvoke(Object object, String methodName, Object[] arguments,
Object result){
if( methodName in this.methods )
{
println "after invoking $methodName"
}
result
}
}
有了上一篇文字的基础,这个拦截器类就比较好理解了。不同的是,在上一篇文字的拦截器类中,需要拦截的方法是固定的,而这个拦截器所需要拦截的方法却是由属性"methods"来确定的。
来看看我们怎么使用这个拦截器类:
def proxy= ProxyMetaClass.getInstance( Foo )
proxy.interceptor= new MethodsInterceptor(methods:['test1','test2'])
proxy.use{
def f= new Foo()
f.test1()
f.test2()
f.test3()
}
运行结果为:
before invoking test1
test1
after invoking test1
before invoking test2
test2
after invoking test2
test3
有了这个客户给定需要拦截的参数,我们就朝着AOP编程迈进了第一步。
接下来,我们需要该拦截器类不同的类起到拦截作用,这样,我们就可以说,我们初步的完成了一个简单的AOP编程。
下面,我们来做一个简单的帮助类,来使得我们的拦截器可以拦截不同的类:
class InterceptorHelper {
def static intercept(Class clzz,methodNames,Closure closure)
{
def proxy= ProxyMetaClass.getInstance( clzz )
proxy.interceptor= new MethodsInterceptor(methods:methodNames)
proxy.use{
closure.call()
}
}
}
很简单,该工具类需要我们输入三个参数:"clzz"需要拦截的对象的类;"methodNames"需要拦截的方法;"closure"调用那些需要拦截的方法。
下面是测试用例:
InterceptorHelper.intercept(Foo,['test1','test2']){
def f = new Foo()
f.test1()
f.test2()
f.test3()
}
运行结果为:
before invoking test1
test1
after invoking test1
before invoking test2
test2
after invoking test2
test3
为了测试上面的工具类是否能够拦截其他的类,我们特意再做了一个测试,假设有如下的一个类:
class Too {
def test1()
{
println 'too1'
}
def test2()
{
println 'too2'
}
}
我们也来测试一下,看看我们的工具类是否能够拦截这个类:
InterceptorHelper.intercept(Too,['test1','test2']){
def t = new Too()
t.test1()
t.test2()
}
运行结果为:
before invoking test1
too1
after invoking test1
before invoking test2
too2
after invoking test2
可以看到,的确可以拦截不同的类的给定方法了。这样,我们就向着AOP编程迈出了关键性的一步了。以后,我们在实际的编码过程中,我们可能会对我们的拦截器类以及AOP工具提出各种各样的要求。但都可以从我们上面的例子扩展开来。
我们可以去试着实现一下:
我们如何实现对我们在客户端给定的对象实现所有的除构造器和GroovyObject方法外的方法实现拦截。
我们又该如何实现对我们在客户端给定的对象实现由客户端给定一些不需要拦截的方法后,拦截其他的方法。
从这样的各种各样的要求可以看出,实现我们自己的AOP编程的确可以根据项目中的实际要求,达到方便灵活的目的。
- Groovy探索之MOP 十 Interceptor 二
- Groovy探索之MOP 九 Interceptor 一
- Groovy探索之MOP 十三 Interceptor 三(1)
- Groovy探索之MOP 十三 Interceptor 三(2)
- Groovy探索之MOP 十六 使用Interceptor实现简单的观察家模式
- Groovy探索之MOP 二 对类属性的各种操作
- Groovy探索之MOP 十四 对Java类使用Groovy语言的MOP
- Groovy探索之MOP 一 invokeMethod和methodMissing方法
- Groovy探索之MOP 三 Class、MetaClass和ExpandoMetaClass
- Groovy探索之MOP 四 使用ExpandoMetaClass来实现Mixin
- Groovy探索之MOP 五 针对接口类型的动态性
- Groovy探索之MOP 十一 运行期内覆盖invokeMethod
- Groovy探索之MOP 十二 方法的调用顺序
- Groovy探索之Builder 二
- Groovy探索之MOP 七 运行期内的方法和属性分析
- Groovy探索之MOP 八 运行期内给类和对象添加属性或方法
- Groovy探索之MOP 六 运行期内添加构造器和静态方法
- Groovy探索之MOP 十五 方法名的动态性(1)
- 写在这里 用以自警!! 这就是现在大学生的软件设计能力
- 一位老兄看 深圳西安北京上海 大众收入与消费
- 看见这十句话,被感动了,也记住了……
- hibernate集合映射inverse和cascade详解(很经典 )
- 正确的生活态度
- Groovy探索之MOP 十 Interceptor 二
- 什么平台给了社区站长机会
- 在SQL中修改视图中的数据会受到哪些限制
- jsp动态生成xml文件
- SVN服务端的安装。
- 原地踏步
- 摩尔斯电码
- 请教一个java中ftp客户端的问题
- 一位软件工程师的6年总结