gpt4 book ai didi

java - AspectJ 和 Spring AOC

转载 作者:行者123 更新时间:2023-12-01 16:31:46 25 4
gpt4 key购买 nike

我正在编写一个自定义 AOP 方法处理程序,并且我有两个具有不同注释的方法:

@AfterReturning(value = "@annotation(Metric)", returning = "response")
public void afterReturning(JoinPoint joinPoint, ResponseWithStatus response) {
final Signature signature = joinPoint.getSignature();
final int responseStatus = response.getStatus();
metrics.putIfAbsent(signature, resultCounters);
if (responseStatus == ResponseStatus.SUCCESS.getValue()) {
System.out.println("Method " + signature + " returned its result");
metrics.get(signature).get(SUCCESS).increment();
} else if (errorCodes.contains(responseStatus)) {
System.out.println("Method " + signature + " returned error");
metrics.get(signature).get(ERROR).increment();
} else if (businessErrorCodes.contains(responseStatus)) {
System.out.println("Method " + signature + " returned business error");
metrics.get(signature).get(BUSINESS_ERROR).increment();
}
}

@AfterThrowing(value = "@annotation(Metric)", throwing = "exception")
public void afterThrowing(JoinPoint joinPoint, Exception exception) {
final Signature signature = joinPoint.getSignature();
final Class<? extends Exception> exceptionClass = exception.getClass();
final String exceptionName = exceptionClass.getName();
metrics.putIfAbsent(signature, resultCounters);
if (exceptionClass.equals(ValidationWebFault_Exception.class)) {
System.out.println("Method " + signature + " threw " + exceptionName);
metrics.get(signature).get(ERROR).increment();
}
}

但是我的方法仅由 @AfterThrowing 方法处理。我如何将它们用于一个注释?

最佳答案

我已经自己解决了这个问题。我只需要在 afterReturning() 方法中使用 Object response 而不是 ResponseWithStatus response

关于java - AspectJ 和 Spring AOC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62026595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com