@AfterReturning 从 ExceptionHandler 不工作
@AfterReturning from ExceptionHandler not working
我有一个 GlobalExceptionHandler class,它包含多个用 @ExceptionHandler 注释的方法。
@ExceptionHandler({ AccessDeniedException.class })
public final ResponseEntity<Object> handleAccessDeniedException(
Exception ex, WebRequest request) {
return new ResponseEntity<Object>(
"Access denied message here", new HttpHeaders(), HttpStatus.FORBIDDEN);
}
我有一个 AOP,它应该在异常处理程序 returns 响应之后触发。
@AfterReturning(value="@annotation(exceptionHandler)",returning="response")
public void afterReturningAdvice(JoinPoint joinPoint, Object response) {
//do something
}
但是在处理程序 returns 有效响应后不会触发 @AfterReturning。
已尝试完全限定名称但无效
@AfterReturning(value = "@annotation(org.springframework.web.bind.annotation.ExceptionHandler)", returning = "response"){
public void afterReturningAdvice(JoinPoint joinPoint, Object response) {
//do something
}
请通过 documentation 了解 Spring 框架中的代理机制。
假设编写的ExceptionHandler
代码格式如下
@ControllerAdvice
public class TestControllerAdvice {
@ExceptionHandler({ AccessDeniedException.class })
final public ResponseEntity<Object> handleAccessDeniedException(
Exception ex, WebRequest request) {
return new ResponseEntity<Object>(
"Access denied message here", new HttpHeaders(), HttpStatus.FORBIDDEN);
}
}
与问题相关的文档中的要点是
Spring AOP 使用 JDK 动态代理或 CGLIB 来创建
给定目标对象的代理。
如果被代理的目标对象至少实现了一个
接口,使用 JDK 动态代理。所有接口
由目标类型实现的被代理。如果目标对象
不实现任何接口,创建 CGLIB 代理。
对于 CGLIB,无法建议使用 final 方法,因为它们无法在 runtime-generated 子类中被覆盖。
- OP 根据评论和提示确定了问题,此答案供以后参考。
我有一个 GlobalExceptionHandler class,它包含多个用 @ExceptionHandler 注释的方法。
@ExceptionHandler({ AccessDeniedException.class })
public final ResponseEntity<Object> handleAccessDeniedException(
Exception ex, WebRequest request) {
return new ResponseEntity<Object>(
"Access denied message here", new HttpHeaders(), HttpStatus.FORBIDDEN);
}
我有一个 AOP,它应该在异常处理程序 returns 响应之后触发。
@AfterReturning(value="@annotation(exceptionHandler)",returning="response")
public void afterReturningAdvice(JoinPoint joinPoint, Object response) {
//do something
}
但是在处理程序 returns 有效响应后不会触发 @AfterReturning。
已尝试完全限定名称但无效
@AfterReturning(value = "@annotation(org.springframework.web.bind.annotation.ExceptionHandler)", returning = "response"){
public void afterReturningAdvice(JoinPoint joinPoint, Object response) {
//do something
}
请通过 documentation 了解 Spring 框架中的代理机制。
假设编写的ExceptionHandler
代码格式如下
@ControllerAdvice
public class TestControllerAdvice {
@ExceptionHandler({ AccessDeniedException.class })
final public ResponseEntity<Object> handleAccessDeniedException(
Exception ex, WebRequest request) {
return new ResponseEntity<Object>(
"Access denied message here", new HttpHeaders(), HttpStatus.FORBIDDEN);
}
}
与问题相关的文档中的要点是
Spring AOP 使用 JDK 动态代理或 CGLIB 来创建 给定目标对象的代理。
如果被代理的目标对象至少实现了一个 接口,使用 JDK 动态代理。所有接口 由目标类型实现的被代理。如果目标对象 不实现任何接口,创建 CGLIB 代理。
对于 CGLIB,无法建议使用 final 方法,因为它们无法在 runtime-generated 子类中被覆盖。
- OP 根据评论和提示确定了问题,此答案供以后参考。