zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    测试流程规范系列(5):BUG提交
    测试流程规范系列(6):测试报告
    测试流程规范系列(7):测试准出
    安全测试系列(1):基本概念
    Linux系列(1):常用命令
    Linux——常用命令详解
    Ant——ant的使用
    java——XML与java对象装换
    SpringMVC——form标签的使用
    SpringMVC案例1——对User表进行CRUD操作
  • 原文地址:https://www.cnblogs.com/lovestart/p/11273306.html
Copyright © 2011-2022 走看看