1. 程式人生 > >Spring-AOP 混合使用各種切面型別及不同切面總結

Spring-AOP 混合使用各種切面型別及不同切面總結

概述

通過我們整個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>在切點表示式中,需要注意不能使用切點函式繫結連線點方法入參,否則會產生錯誤。