`
gelongmei
  • 浏览: 209495 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

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 mvc框架源码

    在本源码分析中,我们将探讨Spring MVC的工作原理、主要组件及其交互方式。 1. **DispatcherServlet**: 作为Spring MVC的前端控制器,DispatcherServlet是所有请求的入口点。它负责拦截请求,根据请求信息(如URL、...

    spring-framework-4.3.12.RELEASE--编译好的源码.rar

    本文将针对该版本的源码进行深入解读,帮助读者理解Spring的核心机制,提升对框架的运用能力。 1. **模块结构** Spring Framework主要分为五个核心模块:Core Container(核心容器)、Data Access/Integration...

    官方原版源码 spring-framework-5.2.9.RELEASE.zip

    二、源码解读 1. **ApplicationContext**:这是Spring的核心接口,负责初始化、加载配置文件,管理Bean的生命周期。源码中可以看到它是如何解析XML或Java配置,创建并管理Bean的。 2. **BeanFactory**:作为...

    spring-framework-4.2.3.RELEASE 源码

    总结,Spring Framework 4.2.3.RELEASE的源码深度解读,能让我们更深入地理解其内部机制,从而更好地利用Spring构建高效、稳定的Java应用程序。无论是IoC容器的实现,还是AOP、MVC的原理,都是开发者在日常工作中不...

    spring-framework-4-reference_spring-framework-4_中文翻译_

    《Spring Framework 4 参考指南》中文翻译版是一份详尽解读Spring Framework 4的文档,旨在帮助中文用户深入理解和应用这一强大的Java应用程序开发框架。Spring Framework是Java开发领域中的核心工具,它提供了一个...

    spring-framework-5.0.8.RELEASE:Spring-5.0.8源码阅读-spring源码阅读

    通过对Spring 5.0.8源码的深度解读,开发者不仅可以了解框架的工作原理,还能学习到设计模式、模块化开发、依赖管理等方面的最佳实践。这将有助于提升代码质量,降低项目风险,并为后续的Spring新版本升级打下坚实的...

    spring-framework-4.1.0.RELEASE

    本篇文章将聚焦于Spring Framework 4.1.0.RELEASE版本,探讨其主要特性、核心组件以及源码解读。 1. **主要特性** Spring 4.1.0.RELEASE引入了多个关键改进和新特性,包括: - **类型安全的`@RequestMapping`...

    spring 源码中文注释

    这份"spring 源码中文注释"包含了对Spring框架源码的详细解读,对于理解其工作原理、优化代码以及定制化开发具有重要意义。 首先,我们来看看Spring的核心组件——IoC容器。IoC容器是Spring的核心,它负责管理对象...

    spring源码分析(1-10)

    Spring 源代码分析系列涵盖了多个关键模块,包括事务处理、IoC容器、JDBC、MVC、AOP以及与Hibernate和Acegi安全框架的集成。以下是对这些知识点的详细阐述: 1. **Spring 事务处理**:Spring 提供了声明式事务管理...

    菜鸟 Spring 源码解读 推荐流程

    Spring框架是Java开发中广泛应用的一个开源...总之,Spring源码解读是一项艰巨但极其有价值的任务,对于提升编程技巧和深入理解软件设计都有极大的帮助。作为"菜鸟",不要畏惧,逐步深入,你将收获满满的知识和经验。

    spring-framework-3.2.10.RELEASE 源码

    九、源码解读的重要性 通过阅读Spring 3.2.10.RELEASE的源码,开发者可以更深入理解其设计思想,掌握核心功能的实现细节,这对于优化代码、排查问题、扩展功能具有重要意义。 总结,Spring Framework 3.2.10....

    spring-framework-2.5.6

    《Spring框架2.5.6深度解析》 ...通过对源码的深入解读,我们可以更透彻地理解Spring的核心机制,同时也能从中汲取经验,应用于现代的开发实践中。每日积累,逐步提升,Spring的世界等待着我们的探索。

    【面试资料】-(机构内训资料)看透Spring MVC源代码分析与实践.zip

    这份【面试资料】-(机构内训资料)看透Spring MVC源代码分析与实践.zip文件很可能是为了帮助求职者准备相关面试问题而设计的,包含了对Spring MVC工作原理、关键组件和源码解读的详尽解析。 1. **Spring MVC基本...

    spring-framework-4.0.7.RELEASE 源码

    三、源码解读 阅读Spring源码有助于深入理解其内部工作原理,例如: - BeanFactory的初始化过程,包括XML配置解析、BeanDefinition的加载和注册。 - AOP的实现机制,包括ProxyFactoryBean的创建、切面匹配策略以及...

    spring-in-action-5-samples-master源代码.zip

    本文将对这些源代码进行深入解读,帮助读者更好地理解和应用Spring框架的核心概念。 首先,让我们关注Spring框架的基础——依赖注入(Dependency Injection,简称DI)。Spring通过DI实现了对象之间的松耦合,使得...

    spring-1.0-rc1.zip

    本篇将聚焦于Spring的初代版本——Spring 1.0-rc1,通过对源码的深入解读,揭示其设计理念和核心功能,帮助读者理解Spring框架的基础架构和实现原理。 Spring 1.0-rc1是Spring框架发展的重要里程碑,它的代码量相对...

    Spring源码-interface21

    5. **MVC框架**:Spring MVC是Spring用于构建Web应用的模块,它提供了一个模型-视图-控制器架构。在源码中,`DispatcherServlet`是入口点,`Controller`接口定义了控制器行为,而`ViewResolver`负责解析视图。 6. *...

    SSM-maven下整合SpringMVC-Spring-Mybatis框架实例源码

    首先,SpringMVC是Spring框架的一个模块,主要负责处理HTTP请求和返回响应,它提供了模型-视图-控制器(MVC)架构模式,帮助开发者构建可维护的Web应用。在SpringMVC中,Controller接收请求,Service层处理业务逻辑...

    Spring MVC应用开源架构源码2021.pdf

    Spring MVC源码深度剖析部分,将会详细解读Spring MVC框架的内部实现机制。核心组件如DispatcherServlet、HandlerMapping、HandlerAdapter等,每个组件的职责和如何相互协作,以及源码中对各种复杂场景的处理。 SSM...

    深入剖析Spring Web源码 pdf高清版(第二版)

    这本书详细解读了Spring MVC和Spring WebFlux两大核心模块的源码,帮助读者理解Spring如何实现高效的Web应用开发。下面将根据标题、描述以及标签,深入探讨Spring MVC和Spring WebFlux的相关知识点。 一、Spring ...

Global site tag (gtag.js) - Google Analytics