javascript
Spring-AOP @AspectJ进阶之绑定抛出的异常
文章目錄
- 概述
- 實例
- 總結(jié)
概述
和通過切點函數(shù)綁定連接點信息不同,連接點拋出的異常必須使用AfterThrowing注解的throwing成員進(jìn)行綁定
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
業(yè)務(wù)類
package com.xgj.aop.spring.advisor.aspectJAdvance.bindException;import org.springframework.stereotype.Component;@Component public class BussinessException {public void dealBussiness(String bussinessName) {System.out.println("dealBussiness executed");// just a demo code ,in fact it's not cautiousif (bussinessName != null && "bug".equals(bussinessName))throw new IllegalArgumentException("iae Exception");elsethrow new RuntimeException("re Exception");} }切面
package com.xgj.aop.spring.advisor.aspectJAdvance.bindException;import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect;/*** * * @ClassName: BindReturnValueAspect* * @Description: @Aspect標(biāo)注的切面,* 和通過切點函數(shù)綁定連接點信息不同,連接點拋出的異常必須使用AfterThrowing注解的throwing成員進(jìn)行綁定* * (1)處throwing指定的異常名和(2)處入?yún)⒌漠惓C嗤?#xff0c;這個異常增強(qiáng)只在連接點拋出的異常instanceof* IllegalArgumentException才匹配,增強(qiáng)方法通過iae參數(shù)可以訪問拋出的異常對象。* * @author: Mr.Yang* * @date: 2017年9月12日 下午5:47:23*/@Aspect public class BindExceptionAspect {// (1)@AfterThrowing(value = "target(com.xgj.aop.spring.advisor.aspectJAdvance.bindException.BussinessException)", throwing = "iae")public void crossCuttingCode(IllegalArgumentException iae) {// (2)System.out.println("----bindException()----");System.out.println("exception:" + iae.getMessage());System.out.println("----bindException()----");} }(1)處throwing指定的異常名和(2)處入?yún)⒌漠惓C嗤?#xff0c;這個異常增強(qiáng)只在連接點拋出的異常instanceof IllegalArgumentException才匹配,增強(qiáng)方法通過iae參數(shù)可以訪問拋出的異常對象。
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"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"><!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類包以及應(yīng)用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindException"/><!-- 基于@AspectJ切面的驅(qū)動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindException.BindExceptionAspect"/></beans>單元測試
package com.xgj.aop.spring.advisor.aspectJAdvance.bindException;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BindExceptionAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindException/conf-bindException.xml");ctx.getBean("bussinessException", BussinessException.class).dealBussiness("bug");} }輸出結(jié)果
2017-09-12 20:26:25,344 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3695de1a: startup date [Tue Sep 12 20:26:25 BOT 2017]; root of context hierarchy 2017-09-12 20:26:25,458 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bindException/conf-bindException.xml] dealBussiness executed ----bindException()---- exception:iae Exception ----bindException()----可見當(dāng)sdealBussiness(“bug”)拋出異常后,異常增強(qiáng)起效,處理完成后,再向外拋出IllegalArgumentException。如果將①處的代碼調(diào)整為dealBussiness(“bug2”)后,再運(yùn)行代碼,將只看到異常輸出的信息,異常增強(qiáng)沒有任何動作,這是因為RuntimeException 不按類型匹配于 IllegalArgumentException,切點不匹配。
總結(jié)
-
通過切點復(fù)合運(yùn)算,你可以定義出各種復(fù)雜的切點,使切點表達(dá)式的能力進(jìn)一步提升。
-
你可以直接使用切點復(fù)合運(yùn)算符對切點函數(shù)進(jìn)行運(yùn)算,也可以通過切點名引用其它命名切點。
-
當(dāng)對同一個連接點織入多個增強(qiáng)時,你必須考慮讓切面類實現(xiàn)Ordered接口,此外還必須合理計劃同一個切面類中增強(qiáng)方法的聲明順序,因為這些信息都會影響到增強(qiáng)的織入順序。
-
在@AspectJ的切點表達(dá)式中,大多數(shù)的切點函數(shù)都可以綁定連接點方法的入?yún)?#xff0c;以便增強(qiáng)方法訪問連接點信息。
-
此外,你也可以簡單地將增強(qiáng)方法的第一個入?yún)?/strong>定義為JoinPoint訪問連接點的上下文。
總結(jié)
以上是生活随笔為你收集整理的Spring-AOP @AspectJ进阶之绑定抛出的异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP @AspectJ进
- 下一篇: Spring-AOP @AspectJ进