Spring-AOP 混合使用各种切面类型及不同切面总结

来源:互联网 发布:ubuntu xampp panel 编辑:程序博客网 时间:2024/06/16 15:30

  • 概述
  • 混合使用各种切面类型
  • 各种切面类型总结

概述

通过我们整个AOP系列的学习,我们可以总结出 4种定义切面的方式:

  • 基于@AspectJ注解的方式

  • 基于<aop:aspect>的方式

  • 基于<aop:advisor>的方式

  • 基于Advisor类的方式

    如果项目采用JDK5.0及以上版本,可以优先考虑使用@AspectJ;

    如果项目只能使用低版本的JDK,则可以考虑使用<aop:aspect>

    如果正在升级一个基于低版本Spring AOP开发的项目,则可以考虑使用<aop:advisor>复用已经存在的Advice类;

    如果项目只能使用低版本的Spring,那么就只能使用Advisor了

此外,值得注意的是一些切面只能使用基于API的Advisor方式进行构建,如基于ControlFlowPointcut的流程切面。


混合使用各种切面类型

Spring虽然提供了4种定义切面的方式,但其底层的实现技术却是一样的,那就是基于CGLib和JDK动态代理,所以在同一个Spring项目中可以混合使用Spring提供的各种切面定义方式。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd">    <!-- 方式一 :使用Advisor API方式实现的流程控制切面 -->    <!--Advisor API 流程切点 指定流程切点的类 和 流程切点的方法 -->    <bean id="controlFlowPointcut" class="org.springframework.aop.support.ControlFlowPointcut">        <constructor-arg type="java.lang.Class"            value="com.xgj.aop.spring.advisor.ControlFlowAdvisor.WaiterDelegate" />        <constructor-arg type="java.lang.String" value="service" />    </bean>    <!--Advisor API 切面 -->    <bean id="controlFlowAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"        p:pointcut-ref="controlFlowPointcut" p:advice-ref="greetingBeforeAdvice" />    <!-- ||||||||||||||||||分隔线|||||||||||||||||| -->    <!-- 方式二: 使用@AspectJ注解方式定义切面 扫描 -->    <aop:aspectj-autoproxy proxy-target-class="true" />    <!-- ||||||||||||||||||分隔线|||||||||||||||||| -->    <aop:config proxy-target-class="true">        <!-- 命名切点 -->        <aop:pointcut expression="execution(* com..*.Waiter.greetTo(..))"            id="beforeAdvice" />        <!-- 方式三: 基于<aop:advisor>的方式 -->        <aop:advisor advice-ref="greetingBeforeAdvice"            pointcut-ref="beforeAdvice" />    </aop:config>    <bean id="greetingBeforeAdvice"        class="com.xgj.aop.spring.advisor.schema.advisor.GreetingBeforeAdvice" />    <aop:config proxy-target-class="true">        <aop:pointcut id="bussinessBindParamProgram"            expression="target(com.xgj.aop.spring.advisor.schema.bindParameter.BussinessBindParam) and args(name,num,..)" />        <!-- 方式四:基于<aop:aspect>的方式 -->        <aop:aspect ref="adviceMethodsBindParam">            <aop:before pointcut-ref="bussinessBindParamProgram"                method="crossCutting" />        </aop:aspect>    </aop:config></beans>

虽然在Spring中可以混合使用各种切面类型达到相同的效果,但是一般情况下并不会在一个项目中同时使用,尽量根据项目的实际需要采用单一的形式,以保证技术的单一性。


各种切面类型总结

我们来对比下4种切面定义方式,本质上是相同的,都是定义切点和增强,不同的只是表现形式

这里写图片描述

从表中,我们可以看出<aop:advisor>其实是<aop:aspect>和Advisor的混血儿,它的切点表示方法和<aop:aspect>相同,增强定义方式则和Advisor相同。

连接点方法入参的绑定方式和Advisor一样,通过增强接口方法入参进行调用,所以<aop:advisor>在切点表达式中,需要注意不能使用切点函数绑定连接点方法入参,否则会产生错误。

原创粉丝点击