说在前面
  
  本次主要介绍springmvc配置解析。
  
  springmvc配置解析
  
  本次介绍MvcNamespaceHandler。
  
  进入到这个方法org.springframework.web.servlet.config.ViewResolversBeanDefinitionParser#parse
  
  public BeanDefinition parse(Element element, ParserContext context) {
  
  Object source = context.extractSource(element);
  
  context.pushContainingComponent(new CompositeComponentDefinition(element.getTagName(), source));
  
  ManagedList<Object> resolvers = new ManagedList<Object>(4);
  
  resolvers.setSource(context.extractSource(element));
  
  // 支持以下视图
  
  String[] names = new String[] {"jsp", "tiles", "bean-name", "freemarker", "velocity", "groovy", "script-template", "bean", "ref"};
  
  for (Element resolverElement : DomUtils.getChildElementsByTagName(element, names)) {
  
  String name = resolverElement.getLocalName();
  
  if ("bean".equals(name) || "ref".equals(name)) {
  
  resolvers.add(context.getDelegate().parsePropertySubElement(resolverElement, null));
  
  continue;
  
  }
  
  RootBeanDefinition resolverBeanDef;
  
  if ("jsp".equals(name)) {
  
  // 注册InternalResourceViewResolver视图解析器bean定义
  
  resolverBeanDef = new RootBeanDefinition(InternalResourceViewResolver.class);
  
  // 视图文件前缀
  
  resolverBeanDef.getPropertyValues().add("prefix", "/WEB-INF/");
  
  // 视图文件后缀
  
  resolverBeanDef.getPropertyValues().add("suffix", ".jsp");
  
  // 添加基于url的视图属性 ->
  
  addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
  
  }
  
  else if ("tiles".equals(name)) {
  
  resolverBeanDef = new RootBeanDefinition(TilesViewResolver.class);
  
  addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
  
  }
  
  else if ("freemarker".equals(name)) {
  
  // 解析FreeMarkerViewResolver bean定义
  
  resolverBeanDef = new RootBeanDefinition(FreeMarkerViewResolver.class);
  
  resolverBeanDef.getPropertyValues().add("suffix", ".ftl");
  
  addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
  
  }
  
  else if ("velocity".equals(name)) {
  
  // 解析VelocityViewResolver bean定义
  
  resolverBeanDef = new RootBeanDefinition(org.springframework.web.servlet.view.velocity.VelocityViewResolver.class);
  
  resolverBeanDef.getPropertyValues().add("suffix", ".vm");
  
  addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
  
  }
  
  else if ("groovy".equals(name)) {
  
  resolverBeanDef = new RootBeanDefinition(GroovyMarkupViewResolver.class);
  
  resolverBeanDef.getPropertyValues().add("suffix", ".tpl");
  
  addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
  
  }
  
  else if ("script-template".equals(name)) {
  
  resolverBeanDef = new RootBeanDefinition(ScriptTemplateViewResolver.class);
  
  addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
  
  }
  
  else if ("bean-name".equals(name)) {
  
  // 解析BeanNameViewResolver bean定义
  
  resolverBeanDef = new RootBeanDefinition(BeanNameViewResolver.class);
  
  }
  
  else {
  
  // Should never happen
  
  throw new IllegalStateException("Unexpected element name: " + name);
  
  }
  
  resolverBeanDef.setSource(source);
  
  resolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  
  resolvers.add(resolverBeanDef);
  
  }
  
  String beanName = VIEW_RESOLVER_BEAN_NAME;
  
  // 解析ViewResolverComposite bean定义
  
  RootBeanDefinition compositeResolverBeanDef = new RootBeanDefinition(ViewResolverComposite.class);
  
  compositeResolverBeanDef.setSource(source);
  
  compositeResolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  
  names = new String[] {"content-negotiation"};
  
  List<Element> contentNegotiationElements = DomUtils.getChildElementsByTagName(element, names);
  
  if (contentNegotiationElements.isEmpty()) {
  
  compositeResolverBeanDef.getPropertyValues().add("viewResolvers", resolvers);
  
  }
  
  else if (contentNegotiationElements.size() == 1) {
  
  // 创建媒体类型视图解析器 ->
  
  BeanDefinition beanDef = createContentNegotiatingViewResolver(contentNegotiationElements.get(0), context);
  
  // 组合视图解析器,解析viewResolvers属性值
  
  beanDef.getPropertyValues().add("viewResolvers", resolvers);
  
  ManagedList<Object> list = new ManagedList<Object>(1);
  
  list.add(beanDef);
  
  // 视图解析器的使用顺序
  
  compositeResolverBeanDef.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
  
  compositeResolverBeanDef.getPropertyValues().add("viewResolvers", list);
  
  }
  
  else {
  
  throw new IllegalArgumentException("Only one <content-negotiation> element is allowed.");
  
  }
  
  if (element.hasAttribute("order")) {
  
  compositeResolverBeanDef.getPropertyValues().add("order", element.getAttribute("order"));
  
  }
  
  // 注册mvcViewResolver bean定义
  
  context.getReaderContext().getRegistry().registerBeanDefinition(beanName, compositeResolverBeanDef);
  
  context.registerComponent(new BeanComponentDefinition(compositeResolverBeanDef, beanName));
  
  context.popAndRegisterContainingComponent();
  
  return null;
  
  }
  
  进入到这个方法org.springframework.web.servlet.config.ViewResolversBeanDefinitionParser#addUrlBasedViewResolverProperties
  
  private void addUrlBasedViewResolverProperties(Element element, RootBeanDefinition beanDefinition) {
  
  if (element.hasAttribute("prefix")) {
  
  beanDefinition.getPropertyValues().add("prefix", element.getAttribute("prefix"));
  
  }
  
  if (element.hasAttribute("suffix")) {
  
  beanDefinition.getPropertyValues().add("suffix", element.getAttribute("suffix"));
  
  }
  
  if (element.hasAttribute("cache-views")) {
  
  beanDefinition.getPropertyValues().add("cache", element.getAttribute("cache-views"));
  
  http://dasheng178.com/#portal/list.html
  
  if (element.hasAttribute("view-class")) {
  
  beanDefinition.getPropertyValues().add("viewClass", element.getAttribute("view-class"));
  
  }
  
  if (element.hasAttribute("view-names")) {
  
  beanDefinition.getPropertyValues().add("viewNames", element.getAttribute(www.365soke.com/"view-names"));
  
  }
  
  }
  
  往上返回到这个方法org.springframework.web.servlet.config.ViewResolversBeanDefinitionParser#createContentNegotiatingViewResolver
  
  private BeanDefinition createContentNegotiatingViewResolver(Element resolverElement, ParserContext context) {
  
  // 注册ContentNegotiatingViewResolver bean定义
  
  RootBeanDefinition beanDef =www.sanxingyLzc.com new RootBeanDefinition(ContentNegotiatingViewResolver.class);
  
  beanDef.setSource(context.extractSource(resolverElement));
  
  beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  
  MutablePropertyValues values www.suoLaieyuLe.com= beanDef.getPropertyValues();
  
  // 默认视图
  
  List<Element> elements = DomUtils.getChildElementsByTagName(www.yongshiyule178.com resolverElement, "default-views");
  
  if (!elements.isEmpty()) {
  
  ManagedList<Object> list = new ManagedList<Object>();
  
  for (Element element : DomUtils.getChildElementsByTagName(elements.get(0), "bean", "ref")) {
  
  list.add(context.getDelegate().parsePropertySubElement(element, null));
  
  }
  
  values.add("defaultViews", list);
  
  }
  
  // 是否开启不接受设置
  
  if (resolverElement.hasAttribute("use-not-acceptable"www.meiwanyule.cn)) {
  
  values.add("useNotAcceptableStatusCode", resolverElement.getAttribute("use-not-acceptable"));
  
  }
  
  // 获取媒体类型管理器bean定义 ->
  
  Object manager = MvcNamespaceUtils.getContentNegotiationManager(context);
  
  if (manager != null) {
  
  values.add("contentNegotiationManager", manager);
  
  }
  
  return beanDef;
  
  }
  
  进入到这个方法org.springframework.web.servlet.config.MvcNamespaceUtils#getContentNegotiationManager
  
  public static Object getContentNegotiationManager(www.michenggw.com ParserContext context) {
  
  String name = AnnotationDrivenBeanDefinitionParser.HANDLER_MAPPING_BEAN_NAME;
  
  if (context.getRegistry(www.yongshi123.cn).containsBeanDefinition(name)) {
  
  BeanDefinition handlerMappingBeanDef = context.getRegistry(www.hengy178.com).getBeanDefinition(name);
  
  // 从RequestMappingHandlerMapping bean定义中获取
  
  return handlerMappingBeanDef.getPropertyValues(www.sanxinyuLevip.com).get("contentNegotiationManager");
  
  }
  
  name = AnnotationDrivenBeanDefinitionParser.CONTENT_NEGOTIATION_MANAGER_BEAN_NAME;
  
  if (context.getRegistry(www.wanmeiyulept.com).containsBeanDefinition(name)) {
  
  return new RuntimeBeanReference(name);
  
  }
  
  return null;
  
  }
  
  往上返回到这个方法org.springframework.web.servlet.config.ViewResolversBeanDefinitionParser#parse
  
  说到最后
  
  本次源码解析仅代表个人观点,仅供参考。

springmvc源码解析MvcNamespaceHandler之<mvc:view-resolvers>的更多相关文章

  1. springMVC源码解析--ViewResolver视图解析器执行(三)

    之前两篇博客springMVC源码分析--ViewResolver视图解析器(一)和springMVC源码解析--ViewResolverComposite视图解析器集合(二)中我们已经简单介绍了一些 ...

  2. SpringMVC源码解析- HandlerAdapter - ModelFactory(转)

    ModelFactory主要是两个职责: 1. 初始化model 2. 处理器执行后将modle中相应参数设置到SessionAttributes中 我们来看看具体的处理逻辑(直接充当分析目录): 1 ...

  3. SpringMVC源码解析- HandlerAdapter - ModelFactory

    ModelFactory主要是两个职责: 1. 初始化model 2. 处理器执行后将modle中相应参数设置到SessionAttributes中 我们来看看具体的处理逻辑(直接充当分析目录): 1 ...

  4. SpringMVC源码解析

    一:springmvc运行过程: 1. dispatcherServlet 通过 HandlerMapping 找到controller2. controller经过后台逻辑处理得到结果集modela ...

  5. springMVC源码解析--ViewResolverComposite视图解析器集合(二)

    上一篇博客springMVC源码分析--ViewResolver视图解析器(一)中我们介绍了一些springMVC提供的很多视图解析器ViewResolver,在开发的一套springMVC系统中是可 ...

  6. 深入了解SpringMVC源码解析

    Spring MVC源码解析 Spring MVC的使用原理其实是通过配置一个Servlet来接管所有的请求,所有的请求由这个Servlet来进行分发处理. 我们可以从web.xml里面看出这一点 & ...

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

    上一篇博客springMVC源码分析--HandlerMethodArgumentResolver参数解析器(一)中我们已经介绍了参数解析相关的东西,并且也提到了HandlerMethodArgume ...

  8. SpringMVC源码解析- HandlerAdapter初始化

    HandlerAdapter初始化时,主要是进行注解解析器初始化注册;返回值处理类初始化;全局注解@ControllerAdvice内容读取并缓存. 目录: 注解解析器初始化注册:@ModelAttr ...

  9. SpringMVC 源码解析

    前言         年初面试时接触到一道面试题,在聊到SpringMVC时提到了SpringMVC的开发者为何要设计父子容器呢,又或者说是父子容器的设计有什么更实际的作用呢?          首先 ...

随机推荐

  1. RabbmitMQ-组成及简单使用

    什么是MQ? MQ全程Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ是消费者-生产者模型的典型代表.一端往消息队列中不断写消息而另一端则可以读取队列中的消息. R ...

  2. PHP实现验证码制作

    captcha.php(PHP产生验证码并储存Session): <?php //开启Session session_start(); //绘制底图 $image = imagecreatetr ...

  3. flask_admin 笔记七 扩展功能

    高级功能 1,开启CSRF保护 要将CSRF保护添加到由ModelView实例生成的表单中,请通过指定form_base_class参数在ModelView子类中使用SecureForm类: from ...

  4. Redis_简单使用

    可基于内存也可持久化的Key-Value(字典, Remote Dictionary Server,远程字典服务器)数据库. 客户端:http://redis.io/clients 命令:http:/ ...

  5. Docker容器学习梳理 - 基础知识(2)

    之前已经总结了Docker容器学习梳理--基础知识(1),但是不够详细,下面再完整补充下Docker学习的一些基础. Docker是个什么东西 Docker是一个程序运行.测试.交付的开放平台,Doc ...

  6. Centos7.3下安装Jumpserver 1.0.0(支持windows组件)

    Jumpserver最新版本支持windows组件,废话不多介绍了,下面直接介绍下部署过程: 0)系统环境 CentOS 7.3 IP: 192.168.10.210 [root@jumpserver ...

  7. 《Linux内核分析》第五周学习笔记

    <Linux内核分析>第五周学习笔记 扒开系统调用的三层皮(下) 郭垚 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.c ...

  8. html5制作导航条

    (1)background-repeat:no-repeat;图片不平铺 (2)使用<ul>和<li>便签,代码简介有序.易于编排. (3)在引入外部css文件时,<li ...

  9. Particle filter for visual tracking

    Kalman Filter Cons: Kalman filtering is inadequate because it is based on the unimodal Gaussian dist ...

  10. Android Studio中的Gradle是干什么的

    作者:ghui链接:https://www.zhihu.com/question/30432152/answer/48239946来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...