javascript
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.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://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>在切點表達式中,需要注意不能使用切點函數綁定連接點方法入參,否則會產生錯誤。
總結
以上是生活随笔為你收集整理的Spring-AOP 混合使用各种切面类型及不同切面总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring JDBC-Spring事务
- 下一篇: Spring JDBC-Spring对事