When we use @ControllerAdvice
and @ExceptionHandler
to catch exceptions globally, does it mean that spring uses aop internally, that is to say, spring will create a cut point inside, which matches all @Controller or @RestController modified method, and the aspect is @Around
or @AfterThrowing
?
Or, there is no such cut point, but if there is such logic in DispatcherServlet
, if an exception is encountered, it will be handed over to the corresponding ExceptionHandler method?
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
@ExceptionHandler(BaseException. class)
public ResponseEntity<?> handleAppException(BaseException ex, HttpServletRequest request) {
//...
}
@ExceptionHandler(value = ResourceNotFoundException. class)
public ResponseEntity<ErrorReponse> handleResourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {
//...
}
}
It seems that there is such a source code:
@Nullable
private Method getMappedMethod(Class<? extends Throwable> exceptionType) {
List<Class<? extends Throwable>> matches = new ArrayList<>();
//Find all exception information that can be handled. MappedMethods stores the correspondence between exceptions and methods for handling exceptions
for (Class<? extends Throwable> mappedException : this. mappedMethods. keySet()) {
if (mappedException. isAssignableFrom(exceptionType)) {
matches. add(mappedException);
}
}
// If it is not empty, it means that there is a method to handle exceptions
if (!matches. isEmpty()) {
// Sort by matching degree from small to large
matches. sort(new ExceptionDepthComparator(exceptionType));
// Return the method that handles the exception
return this.mappedMethods.get(matches.get(0));
}
else {
return null;
}
}
But although this is a method to find the corresponding method, it does not mean that there is no defined cut point, so does the global catch exception use aop-related technology? Is global catching exceptions counted as aop, or is it just related to spring mvc?