spring 基于xml的申明式AspectH中的后置通知的返回值获取

1. 配置文件

    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" />
            <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/>
        </aop:aspect>
    </aop:config>

<aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/>中的returnVal就是返回值的名称

2. 切面类

import org.aspectj.lang.JoinPoint;

public class MyAspect {
    public void myAfterReturning(JoinPoint joinpoint) {
        System.out.println("后置通知");
    }
}

注意该类中的public void myAfterReturning(JoinPoint joinpoint)方法只要加上参数Object returnVal就可以,注意“returnVal”名称必须和配置文件中的相同,不然会报错,如下

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
public void myAfterReturning(JoinPoint joinpoint, Object returnVal) {
        System.out.println("后置通知");
}    

另外:public void myAfterReturning(JoinPoint joinpoint)中的参数类型是JoinPointorg.aspectj.lang.JoinPoint这个包的,不要写成Joinpoint了,不然报错都不好发现

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄