javascript
Spring-AOP @AspectJ进阶之绑定抛出的异常
文章目錄
- 概述
- 實(shí)例
- 總結(jié)
概述
和通過(guò)切點(diǎn)函數(shù)綁定連接點(diǎn)信息不同,連接點(diǎn)拋出的異常必須使用AfterThrowing注解的throwing成員進(jìn)行綁定
實(shí)例
代碼已托管到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)注的切面,* 和通過(guò)切點(diǎn)函數(shù)綁定連接點(diǎn)信息不同,連接點(diǎn)拋出的異常必須使用AfterThrowing注解的throwing成員進(jìn)行綁定* * (1)處throwing指定的異常名和(2)處入?yún)⒌漠惓C嗤?#xff0c;這個(gè)異常增強(qiáng)只在連接點(diǎn)拋出的異常instanceof* IllegalArgumentException才匹配,增強(qiáng)方法通過(guò)iae參數(shù)可以訪問(wèn)拋出的異常對(duì)象。* * @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;這個(gè)異常增強(qiáng)只在連接點(diǎn)拋出的異常instanceof IllegalArgumentException才匹配,增強(qiáng)方法通過(guò)iae參數(shù)可以訪問(wèn)拋出的異常對(duì)象。
配置文件
<?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ū)動(dòng)器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindException.BindExceptionAspect"/></beans>單元測(cè)試
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()----可見(jiàn)當(dāng)sdealBussiness(“bug”)拋出異常后,異常增強(qiáng)起效,處理完成后,再向外拋出IllegalArgumentException。如果將①處的代碼調(diào)整為dealBussiness(“bug2”)后,再運(yùn)行代碼,將只看到異常輸出的信息,異常增強(qiáng)沒(méi)有任何動(dòng)作,這是因?yàn)镽untimeException 不按類型匹配于 IllegalArgumentException,切點(diǎn)不匹配。
總結(jié)
-
通過(guò)切點(diǎn)復(fù)合運(yùn)算,你可以定義出各種復(fù)雜的切點(diǎn),使切點(diǎn)表達(dá)式的能力進(jìn)一步提升。
-
你可以直接使用切點(diǎn)復(fù)合運(yùn)算符對(duì)切點(diǎn)函數(shù)進(jìn)行運(yùn)算,也可以通過(guò)切點(diǎn)名引用其它命名切點(diǎn)。
-
當(dāng)對(duì)同一個(gè)連接點(diǎn)織入多個(gè)增強(qiáng)時(shí),你必須考慮讓切面類實(shí)現(xiàn)Ordered接口,此外還必須合理計(jì)劃同一個(gè)切面類中增強(qiáng)方法的聲明順序,因?yàn)檫@些信息都會(huì)影響到增強(qiáng)的織入順序。
-
在@AspectJ的切點(diǎn)表達(dá)式中,大多數(shù)的切點(diǎn)函數(shù)都可以綁定連接點(diǎn)方法的入?yún)?#xff0c;以便增強(qiáng)方法訪問(wèn)連接點(diǎn)信息。
-
此外,你也可以簡(jiǎn)單地將增強(qiáng)方法的第一個(gè)入?yún)?/strong>定義為JoinPoint訪問(wèn)連接點(diǎn)的上下文。
總結(jié)
以上是生活随笔為你收集整理的Spring-AOP @AspectJ进阶之绑定抛出的异常的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring-AOP @AspectJ进
- 下一篇: Spring-AOP @AspectJ进