0
点赞
收藏
分享

微信扫一扫

springboot异常处理的通用方式5


3、确定每个类对应的异常处理方法
3.1、先从缓存中获取每个类对应的异常处理方法,如没有则新建
3.2、使用exceptionHandlerAdvice进行适配,确定了最终的处理的方法

//```java
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(
@Nullable HandlerMethod handlerMethod, Exception exception) {

Class<?> handlerType = null;

if (handlerMethod != null) {
	// Local exception handler methods on the controller class itself.
	// To be invoked through the proxy, even in case of an interface-based proxy.
	handlerType = handlerMethod.getBeanType();
	ExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(handlerType);
	if (resolver == null) {
		resolver = new ExceptionHandlerMethodResolver(handlerType);
		this.exceptionHandlerCache.put(handlerType, resolver);
	}
	Method method = resolver.resolveMethod(exception);
	if (method != null) {
		return new ServletInvocableHandlerMethod(handlerMethod.getBean(), method);
	}
	// For advice applicability check below (involving base packages, assignable types
	// and annotation presence), use target class instead of interface-based proxy.
	if (Proxy.isProxyClass(handlerType)) {
		handlerType = AopUtils.getTargetClass(handlerMethod.getBean());
	}
}

for (Map.Entry<ControllerAdviceBean, ExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
	ControllerAdviceBean advice = entry.getKey();
	if (advice.isApplicableToBeanType(handlerType)) {
		ExceptionHandlerMethodResolver resolver = entry.getValue();
		Method method = resolver.resolveMethod(exception);
		if (method != null) {
			return new ServletInvocableHandlerMethod(advice.resolveBean(), method);
		}
	}
}

return null;}

举报

相关推荐

0 条评论