久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Spring注解配置AOP導致通知執行順序紊亂解決方案

瀏覽:3日期:2023-08-06 14:04:09

今天在測試Spring的AOP時,發現使用注解配置AOP的方式會導致通知的執行順序紊亂。【最終通知居然在異常通知之前執行了】

Spring注解配置AOP導致通知執行順序紊亂解決方案

測試代碼

(1)定義TargetInterface目標接口

public interface TargetInterface {public abstract void targetProxy();}

(2)定義TargetImpl目標類

@Component('target')public class TargetImpl implements TargetInterface {public void targetProxy() { System.out.println('target proxy ......'); int i = 1/0;//異常}}

(3)定義切面類(內含增強方法)

@Component('myAspect')//定義切面類@Aspect//聲明當前類是切面類public class TargetAspect {//定義切點表達式@Pointcut('execution(* com.ahzyy.target.impl.*.*(..))')public void pt() {}@Before('pt()')public void before() { System.out.println('前置通知......');}@After('pt()')public void after() { System.out.println('最終通知......');}@AfterReturning('pt()')public void afterReturning() { System.out.println('后置通知......');}@AfterThrowing('pt()')public void afterThrowing() { System.out.println('異常通知......');}}

(4)配置applicationContextAnno.xml文件

<!--配置組件掃描的包--><context:component-scan base-package='com.ahzyy'/><!--配置AOP自動代理--><aop:aspectj-autoproxy/>

(5)定義測試類

@RunWith(SpringJUnit4ClassRunner.class)//@ContextConfiguration('classpath:applicationContext.xml')@ContextConfiguration('classpath:applicationContextAnno.xml')public class AopTest {@Autowiredprivate TargetInterface target;@Testpublic void test01() { target.targetProxy();}}

(6)運行結果:

Spring注解配置AOP導致通知執行順序紊亂解決方案

【最終通知在異常通知之前執行了!!!】

(7)解決方法:

(7.1)使用xml配置方式配置AOP;

(7.2)注解使用@Around(環繞通知)方式配置AOP(修改TargetAspect類使用環繞通知);

@Component('myAspect')//定義切面類@Aspect//聲明當前類是切面類public class TargetAspect {//定義切點表達式@Pointcut('execution(* com.ahzyy.target.impl.*.*(..))')public void pt() {}@Around('pt()')public Object aroundNotice(ProceedingJoinPoint pjp) { System.out.println('環繞通知'); Object result = null; before();//前置通知 try { result = pjp.proceed(); afterReturning();//后置通知 } catch (Throwable throwable) {afterThrowing();//異常通知 throwable.printStackTrace(); } after();//最終通知 return result;}public void before() { System.out.println('前置通知......');}public void afterReturning() { System.out.println('后置通知......');}public void afterThrowing() { System.out.println('異常通知......');}public void after() { System.out.println('最終通知......');}}

(7.3)運行結果

Spring注解配置AOP導致通知執行順序紊亂解決方案

[運行順序正確]

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: av大全在线 | 超碰人人99| 国产乱人伦av在线a 天天碰天天操 | 国产精品人人做人人爽 | 青青草久久爱 | 欧美一区二区三区黄色 | 免费亚洲视频 | 欧美性一区二区三区 | 毛片免费看| 色鲁97精品国产亚洲 | 婷五月综合 | 美日韩免费视频 | 国产高清在线精品一区二区三区 | 日韩欧美网址 | 婷婷色综合 | 精品三级在线观看 | 精品一区二区三区久久 | 午夜免费剧场 | 午夜精品91 | 国产一区免费 | 免费国产黄网站在线观看视频 | 成人高清| 亚洲一区二区在线免费观看 | 男人天堂网av | 男人天堂社区 | 天天操导航| 亚洲欧美国产毛片在线 | 久久久久久国产精品 | 久久免费在线观看 | 亚洲精品1 | 亚洲视频在线观看免费 | 久久人人网 | 夜夜精品视频 | 亚洲国产精品va在线看黑人 | 久久不射电影网 | 精品国产乱码久久久久久久软件 | 免费毛片网 | 国产不卡视频 | 久久精品国产免费 | 日韩一区二区在线观看 | 999精品在线 |