标签:roc Plan intercept void 类型 encoding round wing code
一 schema-based异常通知
第一步:创建通知类 :新建一个类实现 throwsAdvice 接口,throwsAdvice接口只是标记接口里面并没有任何方法,必须自己写方法,且必须叫 afterThrowing,afterThrowing方法里面的参数,有两种参数方式, 必须是 1 个或 4 个
注意:参数的异常类型要,切点报的异常类型一致, 已经jar包要和jdk版本兼容(出问题解决了好长事件),因为有时候切点抛出的异常会有多种类型,为了正确接受,建议在异常通知类里面,afterThrowing的参数写成Exception,这样可以保证兼容
package com.airplan.pojo; import org.springframework.aop.ThrowsAdvice; public class ExceptionAdvice implements ThrowsAdvice{ public void afterThrowing(Exception ex) throws Throwable { System.out.println(ex.getMessage()); System.out.println("异常通知执行"); } }
第二种写法:
package com.airplan.pojo; import java.lang.reflect.Method; import org.springframework.aop.ThrowsAdvice; public class ExceptionAdvice implements ThrowsAdvice{ public void afterThrowing(Method m, Object[] args,Object target, Exception ex) { System.out.println("执行异常通知"); } }
第二步配置切入点,切面,通知所在的类
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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"> <bean id="afterAdv" class="com.airplan.pojo.MyAfterAdvice"></bean> <!-- 配置通知所在的类,配置通知 --> <bean id="exceptionAdvice" class="com.airplan.pojo.ExceptionAdvice"> </bean> <!-- 配置切面 --> <aop:config> <!-- 配置切点,特别注意expression的写法 --> <aop:pointcut expression="execution(* com.airplan.pojo.PointCutClass.testFun(..))" id="test"/> <aop:advisor advice-ref="exceptionAdvice" pointcut-ref="test"/> </aop:config> <!-- 配置切点所在的类 --> <bean id="pointCutClass" class="com.airplan.pojo.PointCutClass"></bean> </beans>
测试代码;
package com.airplan.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.airplan.pojo.PointCutClass; public class AopDemo { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); PointCutClass pointCutClass = ac.getBean("pointCutClass",PointCutClass.class); pointCutClass.testFun(); //pointCutClass.throwPointCut(); } }
二 schema-based 环绕通知
第一步:创建环绕通知类
package com.airplan.pojo; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class AroundAdv implements MethodInterceptor{ @Override public Object invoke(MethodInvocation arg0) throws Throwable { System.out.println("环绕-前置"); Object result = arg0.proceed();//放行,调用切点方法 System.out.println("环绕-后置"); return result; } }
第二步配置
<bean id="aroundAdv" class="com.airplan.pojo.AroundAdv"></bean> <!-- 配置切面 --> <aop:config> <!-- 配置切点,特别注意expression的写法 --> <aop:pointcut expression="execution(* com.airplan.pojo.PointCutClass.throwPointCut(..))" id="test"/> <aop:advisor advice-ref="aroundAdv" pointcut-ref="test"/> </aop:config> <!-- 配置切点所在的类 --> <bean id="pointCutClass" class="com.airplan.pojo.PointCutClass"></bean>
第三步测试
package com.airplan.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.airplan.pojo.PointCutClass; public class AopDemo { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); PointCutClass pointCutClass = ac.getBean("pointCutClass",PointCutClass.class); pointCutClass.throwPointCut(); //pointCutClass.throwPointCut(); } }
spring学习 十 schema-based 异常通知,和环绕通知
标签:roc Plan intercept void 类型 encoding round wing code
原文地址:https://www.cnblogs.com/cplinux/p/9739968.html