1. 新建一个类实现 throwsAdvice 接口
1.1 必须自己写方法,且必须叫 afterThrowing
1.2 有两种参数方式
1.2.1 必须是 1 个或 4 个
1.3 异常类型要与切点报的异常类型一致
public class MyThrow implements ThrowsAdvice{
//四个参数
public void afterThrowing(Method m, Object[] args,Object target, Exception ex) {
System.out.println("执行异常通知");
}
//一个参数
public void afterThrowing(Exception ex) throws Throwable {
System.out.println("执行异常通过-schema-base 方式");
}
}
2. 在 ApplicationContext.xml 配置
<bean id="mythrow" class="com.bjsxt.advice.MyThrow"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint" />
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>