springboot08(springmvc自动配置原理)
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自动配置原理)的更多相关文章
- spring boot @EnableWebMvc禁用springMvc自动配置原理。
		
说明: 在spring boot中如果定义了自己的java配置文件,并且在文件上使用了@EnableWebMvc 注解,那么sprig boot 的默认配置就会失效.如默认的静态文件配置路径:&quo ...
 - 9 Web开发——springmvc自动配置原理
		
官方文档目录: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-sp ...
 - SpringBoot自动配置原理
		
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾前面Spring的文章(以学习的顺序排好): S ...
 - 5. SprigBoot自动配置原理
		
配置文件到底能写什么?怎么写? 都可以在SpringBoot的官方文档中找到: 配置文件能配置的属性参照 1.自动配置原理: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功 ...
 - 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
		
1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 ...
 - SpringBoot之自动配置原理
		
我在前面的Helloworld的程序中已经分析过一次,配置原理了: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration 2).@En ...
 - SpringBoot源码学习系列之SpringMVC自动配置
		
目录 1.ContentNegotiatingViewResolver 2.静态资源 3.自动注册 Converter, GenericConverter, and Formatter beans. ...
 - 【SpringBoot】SpringBoot与SpringMVC自动配置(五)
		
本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...
 - SpringBoot自动配置原理学习
		
介绍 构建Springboot项目时我们会创建一个启动类 @SpringBootApplication public class DemoApplication { public static voi ...
 
随机推荐
- react-native构建基本页面6---打包发布
			
签名打包发布Release版本的apk安装包 请参考以下两篇文章: ReactNative之Android打包APK方法(趟坑过程) React Native发布APP之签名打包APK 如何发布一个a ...
 - java面试记录三:hashmap、hashtable、concurrentHashmap、ArrayList、linkedList、linkedHashmap、Object类的12个成员方法、消息队列MQ的种类
			
口述题 1.HashMap的原理?(数组+单向链表.put.get.size方法) 非线程安全:(1)hash冲突:多线程某一时刻同时操作hashmap并执行put操作时,可能会产两个key的hash ...
 - LeetCode136. 只出现一次的数字(异或)
			
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...
 - URLSearchParams/FormData
			
一.URLSearchParams()(很好用,但有一定兼容问题,未来版本的浏览器中该功能的语法和行为可能随对应的标准文档而改变.) URLSearchParams 接口定义了一些实用的方法来处理 U ...
 - PP: Multilevel wavelet decomposition network for interpretable time series analysis
			
Problem: the important frequency information is lack of effective modelling. ?? what is frequency in ...
 - SE篇
			
1. List 和 Set 区别 List 特点:元素有放入顺序,元素可重复 Set 特点:元素无放入顺序,元素不可重复,重复元素会覆盖掉 2. List 和 Map 区别 ...
 - ReviewBoard使用:添加SVN
			
1.登录ReviewBoard,选择“Admin” 2.选择 “Repositores”,点击按钮“Add” 3.填写内容,然后点击按钮“SAVE”保存 Name:仓库名称(自己随意写) Hostin ...
 - (转)进程同步之临界区域问题及Peterson算法
			
转自:http://blog.csdn.net/speedme/article/details/17595821 1. 背景 首先,看个例子,进程P1,P2共用一个变量COUNT,初始值为0 ...
 - RN开发-IDE和API
			
一.开发工具 1.Visual Studio Code:微软IDE,轻量级,只有30+M大小 2.nuclide :仅支持Mac 3.WebStorm : JavaScript开发工具(IDE) 二. ...
 - android TextView 支持长按自由复制
			
因为EditText支持系统的长按自由复制,所以只需要把EditText通过配置达到TextView效果就行了 <EditText android:id="@+id/subject_i ...