springMVC源码解析--HandlerMethodArgumentResolverComposite参数解析器集合(二)

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

完整源码及说明如下:

public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver {  
  
    protected final Log logger = LogFactory.getLog(getClass());  
  
    //保存springMVC提供的所有的参数解析器  
    private final List argumentResolvers =  
            new LinkedList();  
  
    //用于缓存已经查找过的参数解析器  
    private final Map argumentResolverCache =  
            new ConcurrentHashMap(256);  
    //获取所有的参数解析器  
    public List 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 resolvers) {  
        if (resolvers != null) {  
            for (HandlerMethodArgumentResolver resolver : resolvers) {  
                this.argumentResolvers.add(resolver);  
            }  
        }  
        return this;  
    }  
  
}  
转载于:http://blog.csdn.net/qq924862077/article/details/54291812

你可能感兴趣的:(springMVC源码解析--HandlerMethodArgumentResolverComposite参数解析器集合(二))