Spring切面类型比较
1.Spring可以定义4种切面
(1)基于@AspjectJ注解的方式
<aop:aspectj-autoproxy proxy-target-class="true"/>
(2)基于<aop:aspect>的方式(基于Schema)
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <aop:before method="preGreeting" pointcut="target(com.baobaotao.NaiveWaiter) and args(name)" arg-names="name" /> <aop:after-returning method="afterReturning" pointcut="target(com.baobaotao.SmartSeller)" returning="retVal" /> <aop:around method="aroundMethod" pointcut="execution(* serveTo(..)) and within(com.baobaotao.Waiter)" /> <aop:after-throwing method="afterThrowingMethod" pointcut="target(com.baobaotao.SmartSeller) and execution(* checkBill(..))" throwing="iae" /> <!-- <aop:after method="afterMethod" pointcut="execution(* com..*.Waiter.greetTo(..))" /> <aop:declare-parents implement-interface="com.baobaotao.Seller" default-impl="com.baobaotao.SmartSeller" types-matching="com.baobaotao.Waiter+" /> --> <aop:before method="bindParams" pointcut="target(com.baobaotao.NaiveWaiter) and args(name,num,..)"/> </aop:aspect> </aop:config>
(3)基于<aop:advisor>的方式(基于Schema)
<aop:config proxy-target-class="true"> <aop:advisor advice-ref="testAdvice" pointcut="execution(* com..*.Waiter.greetTo(..))"/> </aop:config>
(4)基于Advisor类的方式
<bean id="nameAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" p:advice-ref="greetingAdvice"> <property name="mappedName"> <value>serve*</value> </property> </bean>
如果项目采用JDK5.0,可以优先考虑使用@AspectJ,如果项目只能使用低版本的JDK,可以考虑使用<aop:aspect>;如果正在升级一个基于低版本的Spring AOP开发的项目,可以考虑使用<aop:advisor>复用已经存在的advice类;如果项目只能使用低版本的Spring,只能使用Advisor .
2.切面不同定义方式具体实现比较
@AspectJ | <aop:aspect> | Advisor | <aop:advisor> | |
前置增强 | @Before | <aop:before> | MethodBeforeAdvice | 同Advisor |
后置增强 | @AfterReturning | <aop:after-returning> | AfterReturningAdvice | 同Advisor |
环绕增强 | @Around | <aop:around> | MethodInterceptor | 同Advisor |
抛出异常增强 | @AfterThrowing | <aop:after-throwing> | ThrowsAdvice | 同Advisor |
final增强 | @After | <aop:after> | 无 | 同Advisor |
引介增强 | @DeclareParents | <aop:declare-parents> | IntroductionInterceptor | 同Advisor |
相关推荐
也许不会看见 2020-06-28
zhongliwen 2020-05-11
也许不会看见 2020-05-04
方志朋 2020-05-01
横云断岭 2020-04-17
Justagreenonion 2020-03-07
容数据服务集结号 2020-02-20
itjavashuai 2020-02-14
smalllove 2020-02-14
haidaoxianzi 2020-01-12
neweastsun 2019-12-29
咻pur慢 2019-12-25
neweastsun 2019-11-09
Julywhj 2019-11-06
lttXiangFangJia 2019-06-02
YangHuiLiang 2020-08-06
容数据服务集结号 2020-07-19
zhangdy0 2020-07-04