AOP 面向切面编程、拦截器
AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。它是一种新的方法论,它是对传统OOP编程的一种补充。
OOP是关注将需求功能划分为不同的并且相对独立,封装良好的类,并让它们有着属于自己的行为,依靠继承和多态等来定义彼此的关系;AOP是希望能够将通用需求功能从不相关的类当中分离出来,能够使得很多类共享一个行为,一旦发生变化,不必修改很多类,而只需要修改这个行为即可。
AOP是使用切面(aspect)将横切关注点模块化,OOP是使用类将状态和行为模块化。在OOP的世界中,程序都是通过类和接口组织的,使用它们实现程序的核心业务逻辑是十分合适。但是对于实现横切关注点(跨越应用程序多个模块的功能需求)则十分吃力,比如日志记录,验证。

/*计算器接口*/
public interface Calculator
{
public double add(double num1, double num2) throws Exception;
public double sub(double num1, double num2) throws Exception;
public double div(double num1, double num2) throws Exception;
public double mul(double num1, double num2) throws Exception;
}


/*计算器接口的实现类*/
public class ArithmeticCalculator implements Calculator
{
@Override
public double add(double num1, double num2)
{
double result = num1 + num2;
return result;
} @Override
public double sub(double num1, double num2)
{
double result = num1 - num2;
return result;
} /*示意代码 暂时不考虑除数0的情况*/
@Override
public double div(double num1, double num2)
{
double result = num1 / num2;
return result;
} @Override
public double mul(double num1, double num2)
{
double result = num1 * num2;
return result;
}
}

大多数应用程序都有一个通用的需求,即在程序运行期间追踪正在发生的活动。为了给计算机添加日志功能,ArithmeticCalculator类改变如下:

/*计算器接口的实现类,添加记录日志功能*/
public class ArithmeticCalculator implements Calculator
{
@Override
public double add(double num1, double num2)
{
System.out.println("the method [add()]"+"begin with args ("+num1+","+num2+")");
double result = num1 + num2;
System.out.println("the method [add()]"+"end with result ("+result+")"); return result;
} @Override
public double sub(double num1, double num2)
{
System.out.println("the method [sub()]"+"begin with args ("+num1+","+num2+")");
double result = num1 - num2;
System.out.println("the method [sub()]"+"end with result ("+result+")"); return result;
} /*示意代码 暂时不考虑除数0的情况*/
@Override
public double div(double num1, double num2)
{
System.out.println("the method [div()]"+"begin with args ("+num1+","+num2+")");
double result = num1 / num2;
System.out.println("the method [div()]"+"end with result ("+result+")"); return result;
} @Override
public double mul(double num1, double num2)
{
System.out.println("the method [mul()]"+"begin with args ("+num1+","+num2+")");
double result = num1 * num2;
System.out.println("the method [mul()]"+"end with result ("+result+")"); return result;
}
}

若ArithmeticCalculator规定只能计算正数时,又需要添加参数验证方法:

/*计算器接口的实现类,添加记录日志功能*/
public class ArithmeticCalculator implements Calculator
{
@Override
public double add(double num1, double num2) throws Exception
{
this.argsValidatior(num1);
this.argsValidatior(num2); /*同上*/
} @Override
public double sub(double num1, double num2) throws Exception
{
this.argsValidatior(num1);
this.argsValidatior(num2); /*同上*/
} /*示意代码 暂时不考虑除数0的情况*/
@Override
public double div(double num1, double num2) throws Exception
{
this.argsValidatior(num1);
this.argsValidatior(num2); /*同上*/
} @Override
public double mul(double num1, double num2) throws Exception
{
this.argsValidatior(num1);
this.argsValidatior(num2); /*同上*/
} private void argsValidatior(double arg)throws Exception
{
if(arg < 0)
throw new Exception("参数不能为负数");
}
}

上面的程序一个很直观的特点就是,好多重复的代码,并且当加入越来越多的非业务需求(例如日志记录和参数验证),原有的计算器方法变得膨胀冗长。这里有一件非常痛苦的事情,无法使用原有的编程方式将他们模块化,从核心业务中提取出来。例如日志记录和参数验证,AOP里将他们称为横切关注点(crosscutting concern),它们属于系统范围的需求通常需要跨越多个模块。
在使用传统的面向对象的编程方式无法理想化的模块化横切关注点,程序员不能不做的就是将这些横切关注点放置在每一个模块里与核心逻辑交织在一起,这将会导致横切关注点在每一个模块里到处存在。使用非模块化的手段实现横切关注将会导致,代码混乱,代码分散,代码重复。你想想看如果日志记录需要换一种显示方式,那你要改多少代码,一旦漏掉一处(概率很高),将会导致日志记录不一致。这样的代码很维护。种种原因表明,模块只需要关注自己原本的功能需求,需要一种方式来将横切关注点冲模块中提取出来。
忍无可忍的大牛们提出了AOP,它是一个概念,一个规范,本身并没有设定具体语言的实现,也正是这个特性让它变的非常流行,现在已经有许多开源的AOP实现框架了。本次不是介绍这些框架的,我们将不使用这些框架,而是使用底层编码的方式实现最基本的AOP解决上面例子出现的问题。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标的一种实现。AOP可以使用"代理模式"来实现。

代理模式的原理是使用一个代理将对象包装起来,然后用该代理对象取代原始的对象,任何对原始对象的调用首先要经过代理。代理对象负责决定是否以及何时将方法调用信息转发到原始对象上。与此同时,围绕着每个方法的调用,代理对象也可以执行一些额外的工作。可以看出代理模式非常适合实现横切关注点。
由于本人只了解Java,所以姑且认为代理模式有两种实现方式,一种是静态代理、另一种是动态代理。他们的区别在于编译时知不知道代理的对象是谁。在模块比较多的系统中,静态代理是不合适也非常低效的,因为静态代理需要专门为每一个接口设计一个代理类,系统比较大成百上千的接口是很正常的,静态代理模式太消耗人力了。动态代理是JDK所支持的代理模式,它可以非常好的实现横切关注点。

/*使用动态代理需要实现InvocationHandler接口*/
public class ArithmeticCalculatorInvocationHandler implements InvocationHandler
{
/*要代理的对象,动态代理只有在运行时才知道代理谁,所以定义为Object类型,可以代理任意对象*/
private Object target = null; /*通过构造函数传入原对象*/
public ArithmeticCalculatorInvocationHandler(Object target)
{
this.target = target;
} /*InvocationHandler接口的方法,proxy表示代理,method表示原对象被调用的方法,args表示方法的参数*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
/*原对象方法调用前处理日志信息*/
System.out.println("the method ["+method.getName()+"]"+"begin with args ("+Arrays.toString(args)+")"); Object result = method.invoke(this.target, args); /*原对象方法调用后处理日志信息*/
System.out.println("the method ["+method.getName()+"]"+"end with result ("+result+")"); return result;
} /*获取代理类*/
public Object getProxy()
{
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.getClass().getInterfaces(), this);
}
}

场景类调用:

public class Client
{
public static void main(String[] args) throws Exception
{
/*获得代理*/
Calculator arithmeticCalculatorProxy = (Calculator)new ArithmeticCalculatorInvocationHandler(
new ArithmeticCalculator()).getProxy(); /*调用add方法*/
arithmeticCalculatorProxy.add(10, 10);
}
}

控制台的输出:
the method [add]begin with args ([10.0, 10.0])
the method [add]end with result (20.0)
可以看到使用动态代理实现了横切关注点。

若需要添加参数验证功能,只需要再创建一个参数验证代理即可:

public class ArithmeticCalculatorArgsInvocationHandler implements
InvocationHandler
{
/*要代理的对象,动态代理只有在运行时才知道代理谁,所以定义为Object类型,可以代理任意对象*/
private Object target = null; /*通过构造函数传入原对象*/
public ArithmeticCalculatorArgsInvocationHandler(Object target)
{
this.target = target;
} /*InvocationHandler接口的方法,proxy表示代理,method表示原对象被调用的方法,args表示方法的参数*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
System.out.println("begin valid method ["+method.getName()+"] with args "+Arrays.toString(args)); for(Object arg : args)
{
this.argValidtor((Double)arg);
} Object result = method.invoke(this.target, args); return result;
} /*获取代理类*/
public Object getProxy()
{
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this);
} private void argValidtor(double arg) throws Exception
{
if(arg < 0)
throw new Exception("参数不能为负数!");
}
}

场景类调用:

public class Client
{
public static void main(String[] args) throws Exception
{
/*获得代理*/
Calculator arithmeticCalculatorProxy = (Calculator)new ArithmeticCalculatorInvocationHandler(
new ArithmeticCalculator()).getProxy(); Calculator argValidatorProxy = (Calculator)new ArithmeticCalculatorArgsInvocationHandler(arithmeticCalculatorProxy).getProxy(); /*调用add方法*/
argValidatorProxy.add(10, 10);
}
}

控制台输出:
begin valid method [add] with args [10.0, 10.0]
the method [add]begin with args ([10.0, 10.0])
the method [add]end with result (20.0)
输入一个负数数据:

public class Client
{
public static void main(String[] args) throws Exception
{
/*获得代理*/
Calculator arithmeticCalculatorProxy = (Calculator)new ArithmeticCalculatorInvocationHandler(
new ArithmeticCalculator()).getProxy(); Calculator argValidatorProxy = (Calculator)new ArithmeticCalculatorArgsInvocationHandler(arithmeticCalculatorProxy).getProxy(); /*调用add方法*/
argValidatorProxy.add(-10, 10);
}
}

控制台输出:
begin valid method [add] with args [-10.0, 10.0]
Exception in thread "main" java.lang.Exception: 参数不能为负数!
at com.beliefbetrayal.aop.ArithmeticCalculatorArgsInvocationHandler.argValidtor(ArithmeticCalculatorArgsInvocationHandler.java:46)
at com.beliefbetrayal.aop.ArithmeticCalculatorArgsInvocationHandler.invoke(ArithmeticCalculatorArgsInvocationHandler.java:29)
at $Proxy0.add(Unknown Source)
at com.beliefbetrayal.aop.Client.main(Client.java:14)

不知道你有没有使用过Struts2,这个结构和Struts2的拦截器非常相似,一个个Action对象好比我们的原对象业务核心,一个个拦截器好比是这里的代理,通用的功能实现成拦截器,让Action可以共用,Struts2的拦截器也是AOP的优秀实现。
AOP 面向切面编程、拦截器的更多相关文章
- .net core 3.1 过滤器(Filter) 与中间件与AOP面向切面 与拦截器及其应用
Filter(过滤器) 总共有五种,Authorization Filter,Resource Filter,Exception Filter,Action Filter,Result Filter ...
- C# AOP 面向切面编程之 调用拦截
有时候我们需要在代码中对方法调用进行拦截,并修改参数和返回值,这种操作叫做AOP(面向切面编程) 不过需要注意的是,AOP的效率很慢,在需要高效率场合慎用. 以下是C#的AOP方法: 首先建立一个控制 ...
- AOP 面向切面编程, Attribute在项目中的应用
一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...
- [转] AOP面向切面编程
AOP面向切面编程 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
本文梯子 本文3.0版本文章 代码已上传Github+Gitee,文末有地址 大神反馈: 零.今天完成的深红色部分 一.AOP 之 实现日志记录(服务层) 1.定义服务接口与实现类 2.在API层中添 ...
- AOP(面向切面编程)大概了解一下
前言 上一篇在聊MemoryCache的时候,用到了Autofac提供的拦截器进行面向切面编程,很明显能体会到其优势,既然涉及到了,那就趁热打铁,一起来探探面向切面编程. 正文 1. 概述 在软件业, ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
随机推荐
- MSSQL数库备份与还原脚本(多个库时很方便)
每次通过 Management Studio 的界面操作备份或还原数据库,对于单个数据库还好,要是一次要做多个.那就还是用脚本快些,下面有两段脚本分享一下. ===================== ...
- SQL 语句优化—— (二) 索引的利用
索引是与表或视图关联的磁盘上结构,可以加快从表或视图中检索行的速度.索引包含由表或视图中的一列或多列生成的键.与书中的索引一样,数据库中的索引使您可以快速找到表或索引视图中的特定信息.索引包含从表或视 ...
- 解决Collection <__NSArrayM: 0x7f8168f7a750> was mutated while being enumerated.'
当程序出现这个提示的时候,是因为你一边便利数组,又同时修改这个数组里面的内容,导致崩溃,网上的方法如下: NSMutableArray * arrayTemp = xxx; NSArray * arr ...
- 使用 UML 进行业务建模:理解业务用例与系统用例的相似和不同之处
使用 UML 进行业务建模:理解业务用例与系统用例的相似和不同之处 作者:Arthur V. English 出处:IBM 本文内容包括: 背景 业务用例模型与系统用例模型有什么相似之处? 业 ...
- Csharp多态的实现(接口)
1.什么是接口 接口可以看做是一个标准, 所有继承的子类需要按照接口中声明的方法来 接口用关键字 interface 修饰,接口的名字一般是I.........able ,表示我有什么能力 接口一般是 ...
- windows8 安装IIS 和 添加网站(转)
Internet Information Services(IIS,互联网信息服务),是由微软公司提供的基于运行Microsoft Windows的互联网基本服务.最初是Windows NT版本的可选 ...
- javascript块级作用域
在c/java中,拥有块级作用域的概念,大括号内就是一个块级作用域,在块级作用域内声明的变量,块以外不可见. C语音的块级作用域示例如下: ,two = ; if(one < two){ ; t ...
- Less基础教程
Less基础教程 less是较早出现的css预处理器. LESS API 参考 安装和使用 安装比较简单,通过nmp或bower安装即可. npm install less -g bower inst ...
- 关注SSO
https://wiki.jasig.org/display/CASC/Configuring+the+Jasig+CAS+Client+for+Java+in+the+web.xml 其余的看osc ...
- Android利用Fiddler进行网络数据抓包,手机抓包工具汇总
Fiddler抓包工具 Fiddler抓包工具很好用的,它可以干嘛用呢,举个简单例子,当你浏览网页时,网页中有段视频非常好,但网站又不提供下载,用迅雷下载你又找不到下载地址,这个时候,Fiddler抓 ...