spring-mvc源码解读

费弃使用
DefaultAnnotationHandlerMapping

org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter

RequestMappingHandlerMapping


org.springframework.web.servlet.handler.AbstractHandlerMethodMapping<T>

org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.urlMap

入口 :
class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
定位controller

org.springframework.web.servlet.HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.Match<T>
org.springframework.web.method.HandlerMethod

http://www.springframework.org/schema/context/spring-context-3.2.xsd


org.springframework.beans.factory.support.DefaultListableBeanFactory@331fc69a:
defining beans [
mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,
org.springframework.format.support.FormattingConversionServiceFactoryBean#0,
org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,
org.springframework.web.servlet.handler.MappedInterceptor#0,
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,
org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,
org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0,
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1,
viewResolver,
localeResolver,
accountOauthController,
dealOauthController,
orderOauthController,
dwrLoginController,dwrPhoneController,
chargeSystemController,
systemController,
memberPaySystemWebjspController,
testController,
memberPayWebjspController,
buyPartnerOrderWebserviceController,
couponWebserviceController,
testWebserviceController,
org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,
org.springframework.context.annotation.internalCommonAnnotationProcessor,
org.springframework.context.annotation.internalPersistenceAnnotationProcessor,
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
];
parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@3e91d58b




org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite


//执行请求
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(HttpServletRequest, HttpServletResponse, HandlerMethod)


org.springframework.web.method.annotation.ModelFactory


//解决controller上下文参数传递补充
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(NativeWebRequest, ModelAndViewContainer, Object...)

org.springframework.web.method.support.HandlerMethodArgumentResolverComposite@33125360

org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataBinderFactory@60ee6bef
org.springframework.web.servlet.mvc.method.annotation.ServletResponseMethodArgumentResolver.resolveArgument(MethodParameter, ModelAndViewContainer, NativeWebRequest, WebDataBinderFactory)

//删除request
org.springframework.web.util.WebUtils.clearErrorRequestAttributes(HttpServletRequest)
request.removeAttribute(ERROR_STATUS_CODE_ATTRIBUTE);
request.removeAttribute(ERROR_EXCEPTION_TYPE_ATTRIBUTE);
request.removeAttribute(ERROR_MESSAGE_ATTRIBUTE);
request.removeAttribute(ERROR_EXCEPTION_ATTRIBUTE);
request.removeAttribute(ERROR_REQUEST_URI_ATTRIBUTE);
request.removeAttribute(ERROR_SERVLET_NAME_ATTRIBUTE);


//重点,
org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveArgument(MethodParameter, ModelAndViewContainer, NativeWebRequest, WebDataBinderFactory)
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite@709fd7e8

//参数映射解决统一接口
org.springframework.web.method.support.HandlerMethodArgumentResolver

org.springframework.web.method.support.HandlerMethodArgumentResolverComposite@709fd7e8

{
org.springframework.web.method.HandlerMethod$HandlerMethodParameter@d3ab010b=org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@7fc429b2,
org.springframework.web.method.HandlerMethod$HandlerMethodParameter@d3ab010c=org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@1b8d6b0,
org.springframework.web.method.HandlerMethod$HandlerMethodParameter@d3ab010d=org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver@2fa5e12a
}

[
org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@,
org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.PathVariableMapMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMapMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor@,
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@,
org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver@,
org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver@,
org.springframework.web.method.annotation.RequestHeaderMapMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.ServletCookieValueMethodArgumentResolver@,
org.springframework.web.method.annotation.ExpressionValueMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.ServletResponseMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor@,
org.springframework.web.servlet.mvc.method.annotation.RedirectAttributesMethodArgumentResolver@,
org.springframework.web.method.annotation.ModelMethodProcessor@,
org.springframework.web.method.annotation.MapMethodProcessor@,
org.springframework.web.method.annotation.ErrorsMethodArgumentResolver@,
org.springframework.web.method.annotation.SessionStatusMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.UriComponentsBuilderMethodArgumentResolver@,
org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@,
org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor@
]

[org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor


类LocaleChangeInterceptor为spring-webmvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

// /favicon.ico走的是 DefaultServletHttpRequestHandler !注
java.lang.ClassCastException: org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod

DispatcherServlet
handlerMappings
[
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@9b24a8e,
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@183cb59b,
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@39b6df8b,
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@150e3c47]


org.springframework.security.web.access.ExceptionTranslationFilter
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        try {
            chain.doFilter(request, response);

            logger.debug("Chain processed normally");
        }
        catch (IOException ex) {
            throw ex;
        }
        catch (Exception ex) {
            // Try to extract a SpringSecurityException from the stacktrace
            Throwable[] causeChain = throwableAnalyzer.determineCauseChain(ex);
            RuntimeException ase = (AuthenticationException)
                    throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class, causeChain);

            if (ase == null) {
                ase = (AccessDeniedException)throwableAnalyzer.getFirstThrowableOfType(AccessDeniedException.class, causeChain);
            }

            if (ase != null) {
            //用于保存没有权限访问的请求
                handleSpringSecurityException(request, response, chain, ase);
               
               
//关键流程核心类
org.springframework.web.filter.GenericFilterBean


//spring 核心配置,BEAN定义解析类
org.springframework.beans.factory.xml.BeanDefinitionParser

org.springframework.beans.factory.support.DefaultListableBeanFactory

org.springframework.beans.factory.support.BeanDefinitionRegistry


//命名空间
org.springframework.beans.factory.xml.NamespaceHandlerSupport

org.springframework.beans.factory.config.BeanDefinitionHolder

//spring-security
org.springframework.security.oauth2.config.OAuth2SecurityNamespaceHandler
public class OAuth2SecurityNamespaceHandler extends NamespaceHandlerSupport {

public void init() {
registerBeanDefinitionParser("authorization-server", new AuthorizationServerBeanDefinitionParser());
registerBeanDefinitionParser("resource-server", new ResourceServerBeanDefinitionParser());
registerBeanDefinitionParser("client-details-service", new ClientDetailsServiceBeanDefinitionParser());
registerBeanDefinitionParser("client", new ClientBeanDefinitionParser());
registerBeanDefinitionParser("resource", new ResourceBeanDefinitionParser());
registerBeanDefinitionParser("rest-template", new RestTemplateBeanDefinitionParser());
registerBeanDefinitionParser("expression-handler", new ExpressionHandlerBeanDefinitionParser());
registerBeanDefinitionParser("web-expression-handler", new WebExpressionHandlerBeanDefinitionParser());
}
}

//component-scan
public class ContextNamespaceHandler extends NamespaceHandlerSupport {

public void init() {
registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
registerBeanDefinitionParser("mbean-export", new MBeanExportBeanDefinitionParser());
registerBeanDefinitionParser("mbean-server", new MBeanServerBeanDefinitionParser());
}

}


//WebApplicationContext
org.springframework.web.context.support.XmlWebApplicationContext

org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler

public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter implements BeanFactoryAware,
InitializingBean {

private List<HandlerMethodArgumentResolver> customArgumentResolvers;

private HandlerMethodArgumentResolverComposite argumentResolvers;

private HandlerMethodArgumentResolverComposite initBinderArgumentResolvers;

private List<HandlerMethodReturnValueHandler> customReturnValueHandlers;

private HandlerMethodReturnValueHandlerComposite returnValueHandlers;

private List<ModelAndViewResolver> modelAndViewResolvers;

private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();

private List<HttpMessageConverter<?>> messageConverters;

private WebBindingInitializer webBindingInitializer;

private AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor("MvcAsync");

private Long asyncRequestTimeout;

private CallableProcessingInterceptor[] callableInterceptors = new CallableProcessingInterceptor[] {};

private DeferredResultProcessingInterceptor[] deferredResultInterceptors = new DeferredResultProcessingInterceptor[] {};

private boolean ignoreDefaultModelOnRedirect = false;

private int cacheSecondsForSessionAttributeHandlers = 0;

private boolean synchronizeOnSession = false;

private SessionAttributeStore sessionAttributeStore = new DefaultSessionAttributeStore();

private ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();

private ConfigurableBeanFactory beanFactory;

你可能感兴趣的:(spring)