美文网首页springMVC源码分析
springMVC源码解析--HandlerMethodArg

springMVC源码解析--HandlerMethodArg

作者: carway | 来源:发表于2017-10-16 19:00 被阅读15次

    上一篇博客springMVC源码分析--HandlerMethodArgumentResolver参数解析器(一)中我们已经介绍了参数解析相关的东西,并且也提到了HandlerMethodArgumentResolverComposite,其简单来说就是所有的参数解析器的链表,保存了springMVC提供的所有的参数解析器,采用职责链的模式来完成参数解析器的查找,并完成参数解析生成目标对象。

    完整源码及说明如下:

    public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver {  
      
        protected final Log logger = LogFactory.getLog(getClass());  
      
        //保存springMVC提供的所有的参数解析器  
        private final List<HandlerMethodArgumentResolver> argumentResolvers =  
                new LinkedList<HandlerMethodArgumentResolver>();  
      
        //用于缓存已经查找过的参数解析器  
        private final Map<MethodParameter, HandlerMethodArgumentResolver> argumentResolverCache =  
                new ConcurrentHashMap<MethodParameter, HandlerMethodArgumentResolver>(256);  
        //获取所有的参数解析器  
        public List<HandlerMethodArgumentResolver> getResolvers() {  
            return Collections.unmodifiableList(this.argumentResolvers);  
        }  
      
        //判断参数解析器是否支持参数解析  
        @Override  
        public boolean supportsParameter(MethodParameter parameter) {  
            return (getArgumentResolver(parameter) != null);  
        }  
      
        //返回参数解析器解析后的数据  
        @Override  
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,  
                NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {  
      
            HandlerMethodArgumentResolver resolver = getArgumentResolver(parameter);  
            if (resolver == null) {  
                throw new IllegalArgumentException("Unknown parameter type [" + parameter.getParameterType().getName() + "]");  
            }  
            return resolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);  
        }  
      
        //获取支持某个参数解析的参数解析器  
        private HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {  
            HandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter);  
            if (result == null) {  
                for (HandlerMethodArgumentResolver methodArgumentResolver : this.argumentResolvers) {  
                    if (logger.isTraceEnabled()) {  
                        logger.trace("Testing if argument resolver [" + methodArgumentResolver + "] supports [" +  
                                parameter.getGenericParameterType() + "]");  
                    }  
                    if (methodArgumentResolver.supportsParameter(parameter)) {  
                        result = methodArgumentResolver;  
                        this.argumentResolverCache.put(parameter, result);  
                        break;  
                    }  
                }  
            }  
            return result;  
        }  
      
        //添加参数解析器  
        public HandlerMethodArgumentResolverComposite addResolver(HandlerMethodArgumentResolver resolver) {  
            this.argumentResolvers.add(resolver);  
            return this;  
        }  
        //获取参数解析器集合  
        public HandlerMethodArgumentResolverComposite addResolvers(List<? extends HandlerMethodArgumentResolver> resolvers) {  
            if (resolvers != null) {  
                for (HandlerMethodArgumentResolver resolver : resolvers) {  
                    this.argumentResolvers.add(resolver);  
                }  
            }  
            return this;  
        }  
      
    }  
    
    转载于:http://blog.csdn.net/qq924862077/article/details/54291812

    相关文章

      网友评论

        本文标题: springMVC源码解析--HandlerMethodArg

        本文链接:https://www.haomeiwen.com/subject/pyrluxtx.html