public void etisLogAround(ProceedingJoinPoint joinPoint, EtisLog etisLog) throws Throwable { Object[] args = joinPoint.getArgs(); MethodSignature methodSignature = (MethodSignature) joinPoint.getStaticPart().getSignature(); Method method = methodSignature.getMethod(); String[] paramNames = ((MethodSignature) joinPoint .getSignature()).getParameterNames(); for(String paramName: paramNames) { logger.info("paramName:" +paramName); } try { Object result = joinPoint.proceed(); if(methodSignature instanceof MethodSignature) { final Class<?>[] parameterTypes = methodSignature.getParameterTypes(); for(final Class<?> pt : parameterTypes){ logger.info("Parameter type:" + pt); } } @SuppressWarnings("unchecked") ResponseEntity<CaseOutlineHeader> returnValue = (ResponseEntity<CaseOutlineHeader>) result; result = etisLog.trasactionDetail().toString()+" "+returnValue.getBody().getCode().toString(); } catch (IllegalArgumentException e) { throw e; } }我要更改的是CaseOutlineHeader类。 parameterTypes变量包含我想在ResponseEntity 的标记内传递的类的名称。如果我想传递一个不同的类Name怎么办。我应该如何做才能灵活地接受不同的类名?如果我这样做:ResponseEntity<parameterTypes> returnValue = (ResponseEntity<parameterTypes>) result;它将说错误parameterTypes无法解析为类型。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 问题是您的AOP方法需要将结果转换为某种东西,以便获取需要记录的代码值。必须事先知道一些内容,因为您不能在注释中使用类型参数,因此不能将其传递给AOP方法。这意味着您在AOP中访问的所有方法都必须来自已知接口,如下所示:public interface LogCodeProvider { String getLogCode();}public class CaseOutlineHeader implements LogCodeProvider { @Override public String getLogCode() { return "My Code"; }}然后在您的AOP方法中可以执行以下操作:@SuppressWarnings("unchecked")ResponseEntity<LogCodeProvider> returnValue = ResponseEntity<LogCodeProvider>) result;result = etisLog.trasactionDetail().toString()+" "+returnValue.getBody().getLogCode();在我的示例中,我实现了特殊的方法getLogCode(),该方法返回一个字符串,因此每个类都可以准确地确定要输出的内容。但是,重用结果变量来存储从etisLog.trasactionDetail()返回的值看起来确实令人困惑。 (adsbygoogle = window.adsbygoogle || []).push({});
07-27 13:47