Retrieving parameter values passed to a method when handling exceptions with Spring AOP
In my class method of a class, I want to get the parameter values and the name of the parameters. The names are still ok, if I don't get it, but I need to get the values of the passed parameters, is this possible? (No problem with ptCut expression, method gets called i checked with sysouts)
My Aspect method looks something like this:
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
}
Thanks in advance.
+3
source to share
1 answer
You can use the advice.
This allows you to access the parameter while handling the exception.
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)
}
}
}
+3
source to share