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種切面定義方式,本質上是相同的,都是定義切點和增強,不同的只是表現形式

 

 https://img-blog.csdn.net/20170921114019036?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ3NoYW5nd2Vp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast

 從表中,我們可以看出<aop:advisor>其實是<aop:aspect>和Advisor的混血兒,它的切點表示方法和<aop:aspect>相同,增強定義方式則和Advisor相同。

 

連接點方法入參的綁定方式和Advisor一樣,通過增強接口方法入參進行調用,所以<aop:advisor>在切點表達式中,需要注意不能使用切點函數綁定連接點方法入參,否則會產生錯誤。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM