springboot08(springmvc自动配置原理)

摘要:
"given"+requestedMediaTypes.toString():"";if{if{logger.debug;}returnNOT_ACCEPTABLE_VIEW;}else{logger.debug;returnnull;}}ContentNegotiatingViewResolver用来组合所有的视图解析器的protectedvoidinitServletContext{CollectionmatchingBeans=//利用BeanFactoryUtils工具获取所有的视图解析器对象BeanFactoryUtils.beansOfTypeIncludingAncestors.values();if{this.viewResolvers=newArrayList;for{if(this!

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自动配置原理)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇山寨STL实现之allocatorvuejs点滴下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot入门篇

一    什么是springboot Spring官网:http://spring.io/projects SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品: Springboot不是什么真正意义上的新框架,就像maven整合了所有的jar包,spring boot整合了所有常...

在springmvc中配置jedis(转)

主要学习https://github.com/thinkgem/jeesite。一下代码均参考于此并稍作修改。 1.jedis 首先,需要添加jedis: <!--jedis--> <dependency> <groupId>redis.clients</groupId> &l...

【从零开始学SpringMVC笔记】SpringMVC进阶

@RequestMapping 通过@RequestMapping注解可以定义不同的处理器映射规则。 URL路径映射 @RequestMapping(value="item")或@RequestMapping("/item") value的值是数组,可以将多个url映射到同一个方法 @RequestMapping(value = { "itemList",...

SpringMVC介绍及参数绑定

本节内容: SpringMVC介绍 入门程序 SpringMVC架构 SpringMVC整合MyBatis 参数绑定 SpringMVC和Struts2的区别 一、SpringMVC介绍 1. 什么是SpringMVC Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出...

1.使用POI结合springmvc实现上传

1.引入poi的依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency...

SpringMVC上传视频等大文件

前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方式来学习文件上传的原理与实践。该博客重在实践。 一. Http协议原理简介 HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统。它于1990年提出...