和通过切点函数绑定连接点信息不同,连接点抛出的异常必须使用AfterThrowing注解的throwing成员进行绑定
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
业务类
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 cautious
if (bussinessName != null && "bug".equals(bussinessName))
throw new IllegalArgumentException("iae Exception");
else
throw 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标注的切面,
* 和通过切点函数绑定连接点信息不同,连接点抛出的异常必须使用AfterThrowing注解的throwing成员进行绑定
*
* (1)处throwing指定的异常名和(2)处入参的异常名相同,这个异常增强只在连接点抛出的异常instanceof
* IllegalArgumentException才匹配,增强方法通过iae参数可以访问抛出的异常对象。
*
* @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)处入参的异常名相同,这个异常增强只在连接点抛出的异常instanceof IllegalArgumentException才匹配,增强方法通过iae参数可以访问抛出的异常对象。
配置文件
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindException"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<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 {
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindException/conf-bindException.xml");
ctx.getBean("bussinessException", BussinessException.class)
.dealBussiness("bug");
}
}
输出结果
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()----
可见当sdealBussiness(“bug”)抛出异常后,异常增强起效,处理完成后,再向外抛出IllegalArgumentException。如果将①处的代码调整为dealBussiness(“bug2”)后,再运行代码,将只看到异常输出的信息,异常增强没有任何动作,这是因为RuntimeException 不按类型匹配于 IllegalArgumentException,切点不匹配。