zoukankan      html  css  js  c++  java
  • 面向切面编程

    在传统的编写业务逻辑处理代码时,我们通常会习惯性地做几件事情:日志记录、事务控制及权限控制等,然后才是编写核心的业务逻辑处理代码。当代码编写完成回头再看时,不禁发现,扬扬洒洒上百行代码中,真正用于核心业务逻辑处理才那么几行,如图6-4所示。方法复方法,类复类,就这样子带着无可奈何遗憾地度过了多少个春秋。这倒也罢,倘若到了项目的尾声,突然决定在权限控制上需要进行大的变动时,成千上万个方法又得一一"登门拜访",痛苦"雪上加霜"。

     

    如果能把图6-4中众多方法中的所有共有代码全部抽取出来,放置到某个地方集中管理,然后在具体运行时,再由容器动态织入这些共有代码的话,最起码可以解决两个问题:

    Java EE程序员在编写具体的业务逻辑处理方法时,只需关心核心的业务逻辑处理,既提高了工作效率,又使代码变更简洁优雅。

    在日后的维护中由于业务逻辑代码与共有代码分开存放,而且共有代码是集中存放的,因此使维护工作变得简单轻松。

    面向切面编程AOP技术就是为解决这个问题而诞生的,切面就是横切面,如图6-5所示,代表的是一个普遍存在的共有功能,例如,日志切面、权限切面及事务切面等。

     

    下面我们以用户管理业务逻辑组件UserService的AOP实现过程(见图6-6)为例,深度剖析一下AOP技术的实现原理。AOP技术是建立在Java语言的反射机制与动态代理机制之上的。业务逻辑组件在运行过程中,AOP容器会动态创建一个代理对象供使用者调用,该代理对象已经按Java EE程序员的意图将切面成功切入到目标方法的连接点上,从而使切面的功能与业务逻辑的功能同时得以执行。从原理上讲,调用者直接调用的其实是AOP容器动态生成的代理对象,再由代理对象调用目标对象完成原始的业务逻辑处理,而代理对象则已经将切面与业务逻辑方法进行了合成。

     

    现将图6-6中涉及到的一些概念解释如下。

    切面(Aspect):其实就是共有功能的实现。如日志切面、权限切面、事务切面等。在实际应用中通常是一个存放共有功能实现的普通Java类,之所以能被AOP容器识别成切面,是在配置中指定的。

    通知(Advice):是切面的具体实现。以目标方法为参照点,根据放置的地方不同,可分为前置通知(Before)、后置通知(AfterReturning)、异常通知(AfterThrowing)、最终通知(After)与环绕通知(Around)5种。在实际应用中通常是切面类中的一个方法,具体属于哪类通知,同样是在配置中指定的。

    连接点(Joinpoint):就是程序在运行过程中能够插入切面的地点。例如,方法调用、异常抛出或字段修改等,但Spring只支持方法级的连接点。

    切入点(Pointcut):用于定义通知应该切入到哪些连接点上。不同的通知通常需要切入到不同的连接点上,这种精准的匹配是由切入点的正则表达式来定义的。

    目标对象(Target):就是那些即将切入切面的对象,也就是那些被通知的对象。这些对象中已经只剩下干干净净的核心业务逻辑代码了,所有的共有功能代码等待AOP容器的切入。

    代理对象(Proxy):将通知应用到目标对象之后被动态创建的对象。可以简单地理解为,代理对象的功能等于目标对象的核心业务逻辑功能加上共有功能。代理对象对于使用者而言是透明的,是程序运行过程中的产物。

    织入(Weaving):将切面应用到目标对象从而创建一个新的代理对象的过程。这个过程可以发生在编译期、类装载期及运行期,当然不同的发生点有着不同的前提条件。譬如发生在编译期的话,就要求有一个支持这种AOP实现的特殊编译器;发生在类装载期,就要求有一个支持AOP实现的特殊类装载器;只有发生在运行期,则可直接通过Java语言的反射机制与动态代理机制来动态实现。

     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的优秀实现

  • 相关阅读:
    servlet
    grep命令
    sort排序命令
    shell脚本面试
    查看远端的端口是否通畅3个简单实用案例!
    mail命令
    linux系统优化的方法
    shell数组
    shell函数介绍语法说明及基本例子
    循环结构的多个控制命令对比与实际案例
  • 原文地址:https://www.cnblogs.com/lianghe01/p/5481865.html
Copyright © 2011-2022 走看看