使用Spring AOP处理异常时获取传递给方法的参数值

jav*_*thu 5 java spring exception-handling spring-aop

在我的方面类的方法中,我想获取参数的值和参数的名称。如果我没有得到名称仍然可以,但我需要获取传递的参数的值是否可能?(ptCut 表达式没有问题,我用 sysouts 检查了该方法正在被调用)

我的方面方法是这样的:

    public void excpetionHappened(Exception e) {
    // Log the exception
    // log the name of the method name/signature which caused the exception
    // log the value of the input parameters to the method
    // wrap and throw new exctn

}
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Ral*_*lph 5

您可以使用周围的建议。

这使得在处理异常时访问参数成为可能。

public aspect ExceptionReporterAspect {

    /** The name of the used logger. */
    public final static String LOGGER_NAME = "AspectJExceptionLogger";

    /** Logger used to log messages. */
    private static final Log LOGGER = LogFactory.getLog(LOGGER_NAME);

    pointcut stringRequestHandler() : 
        execution (@RequestMapping Object the.package..*(..));

    Object around(): objectRequestHandler(){
        try {
            return proceed();
        } catch (Exception ex){
            Signature sig = thisJoinPointStaticPart.getSignature();
            Object[] args = thisJoinPoint.getArgs();

            String location = sig.getDeclaringTypeName() + '.' + sig.getName() + ", args=" + Arrays.toString(args);
            LOGGER.warn("(AOP detected) exception within " + location, ex);

               throw(ex)
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)