• java-springmvc 01


    springmvc也是在spring framework中的,不是一个单独的项目

    MVC就是和Tomcat有关。
    01.MVC启动的第一步,启动Tomcat(这个和springboot的run方法启动Tomcat有关)

    02.SpringMVC中,最为核心的就是DispatcherServlet,在启动Tomcat的过程中:

    1. Tomcat会先创建DispatcherServlet对象(这一步在手写springboot框架中已经写到,Tomcat在启动的时候会启动DispatcherServlet)
    2. 然后调用DispatcherServlet对象的init()

    DispatcherServlet加载到Tomcat中,依然还是靠SpringBoot的自动配置DispatcherServletAutoConfiguration。

    实际上该类的逻辑就是定义了四个类,分别是:

    1.DispatcherServletRegistrationCondition:决定是否注册DispatcherServlet到Servlet容器,beanFactory中有,servlet中没有,则加载
    2.DefaultDispatcherServletCondition:决定是否加载DispatcherServlet
    3.DispatcherServletRegistrationConfiguration:负责注册DispatcherServlet到Servlet容器,并进行一些设置。
    4.DispatcherServletConfiguration:负责加载DispatcherServlet并进行一些配置

    @AutoConfigureOrder(-2147483648)
    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnWebApplication(
        type = Type.SERVLET
    )
    //存在DispatcherServlet.class文件才加载
    @ConditionalOnClass({DispatcherServlet.class})
    //在webServerFactory自动配置完成后才加载,防止工厂还没设置好
    @AutoConfigureAfter({ServletWebServerFactoryAutoConfiguration.class})
    public class DispatcherServletAutoConfiguration {
        public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";
        public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";
    
        public DispatcherServletAutoConfiguration() {
        }
        
        //第一个类,决定是否注册DispatcherServlet到web容器
        @Order(2147483637)
        private static class DispatcherServletRegistrationCondition extends SpringBootCondition {
            private DispatcherServletRegistrationCondition() {
            }
    
            public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
                ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
                ConditionOutcome outcome = this.checkDefaultDispatcherName(beanFactory);
                return !outcome.isMatch() ? outcome : this.checkServletRegistration(beanFactory);
            }
    
            private ConditionOutcome checkDefaultDispatcherName(ConfigurableListableBeanFactory beanFactory) {
                List<String> servlets = Arrays.asList(beanFactory.getBeanNamesForType(DispatcherServlet.class, false, false));
                boolean containsDispatcherBean = beanFactory.containsBean("dispatcherServlet");
                return containsDispatcherBean && !servlets.contains("dispatcherServlet") ? ConditionOutcome.noMatch(this.startMessage().found("non dispatcher servlet").items(new Object[]{"dispatcherServlet"})) : ConditionOutcome.match();
            }
    
            private ConditionOutcome checkServletRegistration(ConfigurableListableBeanFactory beanFactory) {
                Builder message = this.startMessage();
                List<String> registrations = Arrays.asList(beanFactory.getBeanNamesForType(ServletRegistrationBean.class, false, false));
                boolean containsDispatcherRegistrationBean = beanFactory.containsBean("dispatcherServletRegistration");
                if (registrations.isEmpty()) {
                    return containsDispatcherRegistrationBean ? ConditionOutcome.noMatch(message.found("non servlet registration bean").items(new Object[]{"dispatcherServletRegistration"})) : ConditionOutcome.match(message.didNotFind("servlet registration bean").atAll());
                } else if (registrations.contains("dispatcherServletRegistration")) {
                    return ConditionOutcome.noMatch(message.found("servlet registration bean").items(new Object[]{"dispatcherServletRegistration"}));
                } else {
                    return containsDispatcherRegistrationBean ? ConditionOutcome.noMatch(message.found("non servlet registration bean").items(new Object[]{"dispatcherServletRegistration"})) : ConditionOutcome.match(message.found("servlet registration beans").items(Style.QUOTE, registrations).append("and none is named dispatcherServletRegistration"));
                }
            }
    
            private Builder startMessage() {
                return ConditionMessage.forCondition("DispatcherServlet Registration", new Object[0]);
            }
        }
        //第二个类,决定是否创建DispatcherServlet对象
        @Order(2147483637)
        private static class DefaultDispatcherServletCondition extends SpringBootCondition {
            private DefaultDispatcherServletCondition() {
            }
    
            public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
                Builder message = ConditionMessage.forCondition("Default DispatcherServlet", new Object[0]);
                ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
                List<String> dispatchServletBeans = Arrays.asList(beanFactory.getBeanNamesForType(DispatcherServlet.class, false, false));
                if (dispatchServletBeans.contains("dispatcherServlet")) {
                    return ConditionOutcome.noMatch(message.found("dispatcher servlet bean").items(new Object[]{"dispatcherServlet"}));
                } else if (beanFactory.containsBean("dispatcherServlet")) {
                    return ConditionOutcome.noMatch(message.found("non dispatcher servlet bean").items(new Object[]{"dispatcherServlet"}));
                } else {
                    return dispatchServletBeans.isEmpty() ? ConditionOutcome.match(message.didNotFind("dispatcher servlet beans").atAll()) : ConditionOutcome.match(message.found("dispatcher servlet bean", "dispatcher servlet beans").items(Style.QUOTE, dispatchServletBeans).append("and none is named dispatcherServlet"));
                }
            }
        }
    
        @Configuration(
            proxyBeanMethods = false
        )
        //负责将DispatcherServlet注册到servlet容器中,即tomcat中
        //原理是创建一个DispatcherServletRegistrationBean对象
        @Conditional({DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition.class})
        @ConditionalOnClass({ServletRegistration.class})
        @EnableConfigurationProperties({WebMvcProperties.class})
        @Import({DispatcherServletAutoConfiguration.DispatcherServletConfiguration.class})
        protected static class DispatcherServletRegistrationConfiguration {
            protected DispatcherServletRegistrationConfiguration() {
            }
    
            @Bean(
                name = {"dispatcherServletRegistration"}
            )
            @ConditionalOnBean(
                value = {DispatcherServlet.class},
                name = {"dispatcherServlet"}
            )
            public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
                //创建DispatcherServletRegistrationBean,
                //包含一个DispatcherServlet和一个urlMapping
                //即DispatcherServlet负责处理的请求路径
                DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, webMvcProperties.getServlet().getPath());
                registration.setName("dispatcherServlet");
                registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
                multipartConfig.ifAvailable(registration::setMultipartConfig);
                return registration;
            }
        }
    
        @Configuration(
            proxyBeanMethods = false
        )
       //负责加载DispatcherServlet
        @Conditional({DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition.class})
        @ConditionalOnClass({ServletRegistration.class})
        @EnableConfigurationProperties({HttpProperties.class, WebMvcProperties.class})
        protected static class DispatcherServletConfiguration {
            protected DispatcherServletConfiguration() {
            }
    
            @Bean(
                name = {"dispatcherServlet"}
            )
            public DispatcherServlet dispatcherServlet(HttpProperties httpProperties, WebMvcProperties webMvcProperties) {
                DispatcherServlet dispatcherServlet = new DispatcherServlet();
                dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
                dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
                dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
                dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
                dispatcherServlet.setEnableLoggingRequestDetails(httpProperties.isLogRequestDetails());
                return dispatcherServlet;
            }
    
            @Bean
            @ConditionalOnBean({MultipartResolver.class})
            @ConditionalOnMissingBean(
                name = {"multipartResolver"}
            )
            public MultipartResolver multipartResolver(MultipartResolver resolver) {
                return resolver;
            }
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    js笔试面试题5道附答案
    mysql主从搭建(docker)
    Go 代码中的文档和注释
    C++运算符重载
    第一行java代码【java基础第二讲】
    python+opencv+深度学习实现二维码识别 计算机竞赛
    【MySQL】索引的作用及知识储备
    Linux提权辅助工具Linux Exploit Suggester
    类复习【C#】
    vue3+Element-plus el-select 下拉表格组件(el-select+el-table结合)
  • 原文地址:https://blog.csdn.net/qq_38757863/article/details/137973594