开发工具:
文件大小: 1mb
下载次数: 0
上传时间: 2019-08-10
详细说明:NULL
博文链接:https://zzc1684.iteye.com/blog/2210510o oli public abstract class AbstractHandlerException Resolver implements HandlerExceptionResolver, Ordered i
private static finat String HEADER PRAGMA="Pragma
private static final String HEADER EXPIRES="Expires"
6789
private static final string HEADER CACHE CONTROL="Cache-Control":
/* Logger available to subc Lasses
protected final Log Logger LogFac tory, getLog(getclass()
private int order= Ordered. LOWEST PRECEDENCE;默认优先级最低
private Set> mappedHandlers:参数 handle支持集合,不在此集合内的 handler不会处里
private Class->[! mappedHandlerclasses;参数 handler的类集合支持数组
若 handler.不再 mappeHandlers中,那么 handle的类型如果在此数组中,那么也会被处理
private Log warnLogger
private boo lean prevent cAching= false;阻止响应数据缓存响应hp头部加入不缓存的头部),默认不阻止
再看下接口的实现
override
blicModelanDviewresolveexception(httpservletrequestrequestHttpservletreSponseresponse
object handler: Exception ex) i
介绍属性的时候并过
g exception, both at debu101e含用管到门时一Cses这2个属性是否支持参数 handler
(shouldApplyTo (request, handler))i
f (Logger. isDebug Enabled()I
andler Classes这2个属性都为mu,那么全部支持
Logger debug ("Resolving exception from handler [" handler +"]:"+ ex)
eXception( ex, request)日志记录
prepareResponse(ex, response):应头部是否支持缓存
return doReso LveException (request, response, handler, ex):
抽象方法,由子类实现
else i
return null:
3. AbstractHandlerMethodExceptionResolver抽象类
然承 AbstractHandlerExceptionResolver抽象类的抽象类。该类主要就是为 Handlermethod类服务,既 handler参数是
HandlerMethod类型。
该类重写了 shouldapplyto方法
11 ofg protected boolean shouldapp ly to(httP Servletrequest request Object handler)i
lf Chandler = nulL) t
return super, shoul dApplyTo(request, handler)
lse if Chandler ins
f Hand le mEthod[
HandlerMethod hand lerMe thod =(Hand lerMe thod) handler使用 dlerMethoe的ea属性生,这个bean
handler hand LerMe thod getBean (
) return super, shouldApplyTo (request, handler):
般都是Stng类型(实例名称或实例
lse
return false
doresolveException抽象方法的实现中调用了 doresolveHandlerMethodException方法,该方法也是1个抽象方法
4. ExceptionHandlerExceptionResolver 3e
继承自 AbstractHandlerMethodExceptionResolver,该类主要处理 Controller中用 ExceptionHandler注解定义的方法。
该类也是< annotation- driven/>配置中定义的 HandlerExceptionResolver实现类之一,大多数异常处理都是由该类操作。
该类比较重要,我们来详细讲解一下
首先我们看下这个类的属性:
public class ExceptionHandlerExceptionResolver extends AbstractHandlerMe thodExceptionReso lver implements
InitializingBean, ApplicationcontextAware i
private ListcHandLerMe thodArgumentResolver> customArgumentResolvers;自定义 HandlerMethodArgumentResolver集合
private ListcHandlerMethodReturn Handler> customRe turnVa lueHandlers;自定义 HandlerMethodReturnvaluehandle集合
privateListchttpmessageconVerterc?>messageConverters:Htp消息转换器集合
private ContentNegotiationManager conten tNegotiationManager= new CantentNegotiationManager():
88d
缓存
private finaL Map, Ex ception HandlerMe thodReso Lvers exceptionHandlercache
new ConcurrentHashHap<->(64);
key一般存储 Controller
vae是 ExceptionHandlerMethodResolver类
88888
private final Map exceptionHand LerAdyice Cache
new LinkedHashMap<->():
缓存
private HandLerMethodArgumentReso LverComposite argumen tResolvers:
key存储 ControllerAdviceBean
90
rivate HandlerMethodRe turn Va ueHandLercomposite returnvalueHandlers
vaue是 Excepti onH and lerMethodResolver类
private Applicationcontext app licationcontext:
再来看下该类的 doResolveHandlerMethodException抽象方法的实现:
override
protected Mode lAndview doReso lveHandleryethod Exception(Ht tp ServletRequest request
Http Serv LetresponSe response Handlermethod handlermethod, Exception exception)t
ServletInvocableHandlerMe thod exceptionHandlerMe thod- getEx ceptionHandler Method thandlerMe thod, exception); lServletInvocablel
If (exceptionHand Le rMe thod = nut) i
return null
这个 Handlermethod中的 Method是
exceptionHandlerMethod, setHandlerMethodArgumentResolvers(this, argumentResolvers):
给 ServletlnyocableHandlerMethod注入
excep LiwnHaldlerMe Lhod, se LHal er Me thodE luImValueHaldlers(this, re LurnValueHandlers): Handler Method ArgumentResolverComposit
HandlerMethod ReturnValueHandlerCompos
B33
ServletWebRe quest webRequest new Serv LetwebRequest(request, response )
主要就是为了处理
Mode l AndvievContainer may Container new Mode l AndviewContainer(
Handlermethod中方法的参数以及 Handler
ry t
if (logger. isDebug led ())t
logger debug("Invoking ExceptionHandler method:"+ exceptionHandlerMethod
ervletlnvocableHardlerMethodia invokeAndHandle
exceptionHandlerMe thod. invoke And Handle( webRequest, may Container; exception)参数和返回值进行处理,通过 ModelAndviewconta
catch (Exception invocationEx)t
将一些视图名,参数丢入到 Mode lAndviewcontainer
Logger, error("Failed to invoke aExceptionHandLer method:"+ exceptionHandlerMethod, invocationEx):
return null
if(mav Container, isReques tHandledo))t
return new Mode lAndview(
else i
Mode LAnd View mav= new Mo de lAndview().addAllObjec ts(mav Container. getModel():
mav, setViewName(mavContainer get ViewName ());
通过 ModelAndviewcontainer中间变量处理结果
f(! mav Container, isviewReference0)I
mav. setView((View) mav Container. getview()):
return mav:
默认的 HandlerMethodArgument resolver集合
protected List getDefaultArgumentResolvers()t
ist0:
/ Type-based argument resolution
resolvers, add (new Serv letRequestMe thodArgument Resolver());
resolvers, add (new Serv letResponseMethod lver()
/ Custom arguments
if (get customArgumentResolvers()!= null) t
resolvers, addAll(get cus tomArgumentResolvers());
e turn resolvers
默认的 HandlerMethodReturnvaluehandler集合
0 protected List sHandlerMethodRe turnValueHandLer> ge faul tReturn Value Handlers(i
List handlerType=( hand lerMe thod=mut)? handlerY thod, getBeanType O:mu得到 andlerMethod中的ean类型,即 Control
f (handlerMethod ! null i
ExceptionHandLerMethodResolver reso lver this, exceptionHandLercache get (handlerType
if (resolver == null) i
先从缓存中拿 ExceptionHandlerMetho
resolver= new ExceptionHand LerMethodResolver (handLerType):
this, exceptionHandler Cache, put (handlerType, resolver);
缓存中没有的话,实例化1只,然后
377
之后通过 ExceptonHandlerMethodRes
Me thod method resolver, reso lveMe thod (exception);
esolveMethod方法得到1个 Method
f (method nult i
就是处理异常的那个 Method
return new Se letInvocab leHand LerMe thod (hand rMe thod, getBean(), method)y
for (Entry entry: this, exceptionHandlerAdvice Cache, entry Set())I
if(entry getKey (),isApplicableToBeanType (handlerType))f
ExceptionHandlerMe thodResolver resolver= entry, getvalue()
386
Method method= reso lver. resolve Me thod (exception):
如果没有找到处异常的方法
If (me thod != null)[
exceptionHandlerAdviceCache
return new ServLetInvocableHand lerMe thod (entry, getKey (). reso lveBean () method):
这个缓存是根据 Contolled
3
return nult
我们看到 getExceptionHandlerMethod中会实例化 ExceptionHandlerMethodResolver,我们看看这个类到底是什么东西?
ExceptionHandlerMethodResolver是一个会在 Class及 Class的父类集合中找出带有 ExceptionHandler注解的类,该类带有key
为 Throwable, value为 Method的缓存属性。
ExceptionHandler MethodResolver的构造过程
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.