————————————————————————————————————————————————————————————-
获取参数的两种方式:
方式一:
首先要求原始方法有参
使用形参JoinPoint jp,jp.getArgs()获取到参数数组
ps:类似于父组件传参给子组件
public class AOPAdvice {public void before(JoinPoint jp) {Object[] args = jp.getArgs();System.out.println("before..."+args[0]);}public void after(JoinPoint jp) {Object[] args = jp.getArgs();System.out.println("afterbefore..."+args[0]);}}
方式二(不推荐):
<aop:config><aop:pointcut id="pt" expression="execution(* *..*(..))"/><aop:aspect ref="myAdvice"><aop:after method="before1" pointcut="execution(* *..*(int)) && args(x)" /></aop:aspect></aop:config>
使用args(x),x必须和通知方法形参名一致(配置文件和代码强绑定)。
————————————————————————————————————————————————————————————-
获取返回值:
afterReturing(返回值后运行)
在通知消息中使用形参Object ret
public void afterReturing(Object ret) {System.out.println("afterReturing...."+ret);}
配置文件
使用returning
<aop:after-returning method="afterReturing" pointcut-ref="pt" returning="ret"/>
around(环绕)
public Object around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("around before");// 对原始方法的调用Object ret = pjp.proceed();System.out.println("around after"+ret);return ret;}
————————————————————————————————————————————————————————————-
获取返异常:
after-throwing(报错后运行)
配置文件中使用throwing参数
<aop:after-throwing method="afterThrowing" pointcut-ref="pt" throwing="t"/>
消息通知里使用Throwable t形参获取
public void afterThrowing(Throwable t) {System.out.println("afterThrowing"+t.getMessage());}
around(环绕)
直接捕获异常操作
public Object around(ProceedingJoinPoint pjp){System.out.println("around before");// 对原始方法的调用Object ret = null;try {ret = pjp.proceed();} catch (Throwable throwable) {System.out.println("around...exception..."+throwable.getMessage());}System.out.println("around after..."+ret);return ret;}
