MVC

WebMvcAutoConfiguration.java

@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(beanFactory.getBean(ContentNegotiationManager.class));
// ContentNegotiatingViewResolver uses all the other view resolvers to locate
// a view so it should have a high precedence
resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
return resolver;
} //解析视图
public View resolveViewName(String viewName, Locale locale) throws Exception {
RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
List<MediaType> requestedMediaTypes = getMediaTypes(((ServletRequestAttributes) attrs).getRequest());
if (requestedMediaTypes != null) {
//获取候选的视图对象
List<View> candidateViews = getCandidateViews(viewName, locale, requestedMediaTypes);
//获取最适合的视图对象
View bestView = getBestView(candidateViews, requestedMediaTypes, attrs);
if (bestView != null) {
return bestView;
}
} String mediaTypeInfo = logger.isDebugEnabled() && requestedMediaTypes != null ?
" given " + requestedMediaTypes.toString() : ""; if (this.useNotAcceptableStatusCode) {
if (logger.isDebugEnabled()) {
logger.debug("Using 406 NOT_ACCEPTABLE" + mediaTypeInfo);
}
return NOT_ACCEPTABLE_VIEW;
}
else {
logger.debug("View remains unresolved" + mediaTypeInfo);
return null;
}
}

ContentNegotiatingViewResolver用来组合所有的视图解析器的

protected void initServletContext(ServletContext servletContext) {
Collection<ViewResolver> matchingBeans =
//利用BeanFactoryUtils工具获取所有的视图解析器对象
BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
if (this.viewResolvers == null) {
this.viewResolvers = new ArrayList<>(matchingBeans.size());
for (ViewResolver viewResolver : matchingBeans) {
if (this != viewResolver) {
this.viewResolvers.add(viewResolver);
}
}
}

如何修改springboot的默认配置

模式:

​ 1、springboot在自动配置很多组件的时候,先看容器中有没有用户自己配置的,如果有用户自己配置的组件,那么就使用用户自己制定的,如果没有,则使用默认的配置;有些组件可以有很多,springboot将用户配置的和默认的组合起来,一起使用。

​ 2、利用configruation的注解进行配置

public class MyMvcConfig extends WebMvcConfigurationSupport
//需要继承这个类来实现一些配置类的重写
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(" ").setViewName("success");
}
}

扩展springMvc

添加@EnableWEebMvc 将全面接管mvc

springboot08(springmvc自动配置原理)的更多相关文章

  1. spring boot @EnableWebMvc禁用springMvc自动配置原理。

    说明: 在spring boot中如果定义了自己的java配置文件,并且在文件上使用了@EnableWebMvc 注解,那么sprig boot 的默认配置就会失效.如默认的静态文件配置路径:&quo ...

  2. 9 Web开发——springmvc自动配置原理

    官方文档目录: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-sp ...

  3. SpringBoot自动配置原理

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾前面Spring的文章(以学习的顺序排好): S ...

  4. 5. SprigBoot自动配置原理

      配置文件到底能写什么?怎么写? 都可以在SpringBoot的官方文档中找到: 配置文件能配置的属性参照   1.自动配置原理: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功 ...

  5. 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)

    1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置  1.命令行参数 所有的配置都可以在命令行上进行指定 ...

  6. SpringBoot之自动配置原理

    我在前面的Helloworld的程序中已经分析过一次,配置原理了: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration 2).@En ...

  7. SpringBoot源码学习系列之SpringMVC自动配置

    目录 1.ContentNegotiatingViewResolver 2.静态资源 3.自动注册 Converter, GenericConverter, and Formatter beans. ...

  8. 【SpringBoot】SpringBoot与SpringMVC自动配置(五)

    本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...

  9. SpringBoot自动配置原理学习

    介绍 构建Springboot项目时我们会创建一个启动类 @SpringBootApplication public class DemoApplication { public static voi ...

随机推荐

  1. ps导出svg

    svg初识 看到一些复杂的svg图形很好奇是手写出来的么,看了源码望而却步.网上看到ps都可以导出svg,然后试了下. 需要加载一个脚本,js写的,把文件复制到路径后重启ps,修改图形名字,然后保存成 ...

  2. cookie、session和application

    https://cloud.tencent.com/developer/article/1493869 前言: 一直想写一篇关于cookie和session的博客,由于种种原因,一直没有整理,这不,今 ...

  3. 第70届(2019)IMO中国国家队选拔考试试题

    (几何除外)1,几何,略2,给定n≥3,是否存在无穷个2n元集{a1,...,an,b1,...,bn}满足其中元素整体互素,a1,...,an成等差数列,b1,...,bn也成等差数列.3,给定k, ...

  4. [CF527D] Clique Problem - 贪心

    数轴上有n 个点,第i 个点的坐标为xi,权值为wi.两个点i,j之间存在一条边当且仅当 abs(xi-xj)>=wi+wj. 你需要求出这张图的最大团的点数. Solution 把每个点看作以 ...

  5. 静态区间第k小 - 整体二分

    蒟蒻终于学会整体二分啦! 思路 实现 丑陋无比的代码 #include <bits/stdc++.h> using namespace std; const int N = 200005; ...

  6. Django文件夹

    Django文件 App文件夹 migrations文件 生成models创建表的翻译语句 telemplatetags文件夹 telemplatetags文件夹下的文件专门用来创建自定义标签.自定义 ...

  7. c# 让接口实现方法

    interface IMy { } static class MyFunc { public static void Func<T>(this T obj) where T : IMy { ...

  8. ❀❀ selenium 学习网站 ★★★★★

    http://edu.51cto.com/course/course_id-7320.html   Selenium IDE WEB自动化测试入门视频课程(上)(共10课时)_在线自学视频教程_51C ...

  9. decimal与float和double的区别

    一直很奇怪C#的预定义数据类型中为什么加了一个decimal,有float和double不就够了吗?今天来挖一挖. 浮点型 Name CTS Type De script ion Significan ...

  10. 测试并整理的 Airpods Pro 刻字可用的最全特殊符号

    天气符号 ☉ 文化符号 卍卐 办公室符号 № ℗ ℡ ℀ ℅ ™ © ® ‰ ¶ § 技术符号 ⇧  打勾符号 ˇ ∨ √ 人的符号 ♀ ♂ ヅ ツ ü 星星符号 ☆ ★ ✽ 箭头符号 ↖ ↗ ↘ ...