spring 基于XML的申明式AspectJ通知的执行顺序
spring 基于XML的申明式AspectJ通知的執(zhí)行順序
關(guān)于各種通知的執(zhí)行順序,結(jié)論:與配置文件中的申明順序有關(guān)
1. XML文件配置說明
圖片來源:《Java EE企業(yè)級應(yīng)用開發(fā)教程》
2. 各種通知說明
前置通知
在執(zhí)行方法之前執(zhí)行
后置通知
在方法返回后執(zhí)行
環(huán)繞通知
在方法前和后執(zhí)行
異常通知
在方法拋出異常后執(zhí)行
最終通知
在方法后執(zhí)行
引介通知
略
注意后置通知和最終通知的區(qū)別:后置通知時在方法成功執(zhí)行后會執(zhí)行的,如果出現(xiàn)異常就不執(zhí)行。而最終通知時無論是否出現(xiàn)異常都會執(zhí)行的,感覺類似于finally
3. 在配置同一個切入點且不出現(xiàn)異常時的執(zhí)行順序
注意,橢圓中不區(qū)分順序
4.具體順序與配置文件的申明順序有關(guān)
- 情況一
<aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config> 順序:
環(huán)繞通知:前
前置通知
doSomething
環(huán)繞通知:后
后置通知(對應(yīng)myAfterReturning)
最終通知
- 情況二
<aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config> 順序:
前置通知
環(huán)繞通知:前
doSomething
環(huán)繞通知:后
后置通知
最終通知
結(jié)論一:前置通知和環(huán)繞通知的順序和申明順序有關(guān),申明在前的先執(zhí)行
- 情況三
當(dāng)before在around前,后置和最終通知都在around后的時候
<aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config> 順序
前置通知
環(huán)繞通知:前
doSomething
環(huán)繞通知:后
最終通知
后置通知
- 情況四
在三的前提條件下交換后置和最終的順序,那么結(jié)果中的最終和后置的順序也會交換
- 其他情況
當(dāng)before和around的申明順序變化時還會有不同以上的規(guī)律,這里就不一一列舉的
總結(jié)
各種通知的執(zhí)行順序可能都不相同,情況有各種各樣,但是只要配置的方法一樣那么執(zhí)行的順序肯定是固定的
出錯的方法的通知順序也是和配置有關(guān)
以下是代碼,供測試使用
相關(guān)依賴包下載
UserDao
public interface UserDao {String doSomething();
}
UserDaoImp
public class UserDaoImp implements UserDao{@Overridepublic void doSomething() {System.out.println("doSomething");}
}
MyAspect
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.JoinPoint;public class MyAspect {public void myBefore(JoinPoint joinpoint) {System.out.println("前置通知");}public void myAfterReturning(JoinPoint joinpoint, Object returnVal) {System.out.println("后置通知");}public Object myAround(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {System.out.println("環(huán)繞通知:前");Object object = proceedingJoinPoint.proceed();System.out.println("環(huán)繞通知:后");return object;}public void myAfterThrowing(JoinPoint joinpoint, Throwable e) {System.out.println("異常:" + e.getMessage());}public void myAfter() {System.out.println("最終通知");}
} Test
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationAspectJ.xml");UserDao userDao = (UserDao) applicationContext.getBean("userDao");userDao.doSomething();}} applicationAspectJ.xml
配置文件中的<aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" />中的springAspectJ改成你的包的路徑
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><bean id="userDao" class="springAspectJ.UserDaoImp"></bean><bean id="myAspect" class="springAspectJ.MyAspect"></bean><aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config>
</beans>
轉(zhuǎn)載于:https://www.cnblogs.com/d-i-p/p/10676343.html
總結(jié)
以上是生活随笔為你收集整理的spring 基于XML的申明式AspectJ通知的执行顺序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 油电混合汽车多人买吗?油电混合汽车和普通
- 下一篇: JDBC学习DayTwo