Use MessageSource for @ExceptionHandler methods

Follow-up for commit bb816c123c

See gh-27203
This commit is contained in:
Yanming Zhou
2021-07-23 10:00:23 +08:00
committed by Rossen Stoyanchev
parent 5b3f11c543
commit f2be4e9320
5 changed files with 72 additions and 3 deletions

View File

@@ -483,7 +483,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
}
Method method = resolver.resolveMethod(exception);
if (method != null) {
return new ServletInvocableHandlerMethod(handlerMethod.getBean(), method);
return new ServletInvocableHandlerMethod(handlerMethod.getBean(), method, this.applicationContext);
}
// For advice applicability check below (involving base packages, assignable types
// and annotation presence), use target class instead of interface-based proxy.
@@ -498,7 +498,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
ExceptionHandlerMethodResolver resolver = entry.getValue();
Method method = resolver.resolveMethod(exception);
if (method != null) {
return new ServletInvocableHandlerMethod(advice.resolveBean(), method);
return new ServletInvocableHandlerMethod(advice.resolveBean(), method, this.applicationContext);
}
}
}

View File

@@ -25,6 +25,7 @@ import java.util.concurrent.Callable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.MessageSource;
import org.springframework.core.KotlinDetector;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
@@ -68,6 +69,13 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
@Nullable
private HandlerMethodReturnValueHandlerComposite returnValueHandlers;
/**
* Variant of {@link #ServletInvocableHandlerMethod(Object, Method)} that
* also accepts a {@link MessageSource}.
*/
public ServletInvocableHandlerMethod(Object handler, Method method, @Nullable MessageSource messageSource) {
super(handler, method, messageSource);
}
/**
* Creates an instance from the given handler and method.