• 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

  • 相关阅读:
    lombok @Builder注解使用的例子、反编译之后的代码详解
    InfluxDB时序数据库应用场景
    阿里巴巴为什么不用 ZooKeeper 做服务发现?
    使用RestTemplate调用接口上传文件
    当try-catch-finally代码块遇上return,代码执行流程是怎样
    Spring Boot 中application.yml与bootstrap.yml的区别
    Java文件上传:Restful接口接收上传文件,缓存在本地
    线程执行synchronized同步代码块时再次重入该锁过程中抛异常,是否会释放锁
    AMQP协议详解与RabbitMQ,MQ消息队列的应用场景,如何避免消息丢失等消息队列常见问题
    数据库:内联接,外联接,空值和联接
  • 原文地址:https://www.cnblogs.com/lovestart/p/11273306.html
Copyright © 2020-2023  润新知