• 【SpringBoot从入门到精通】01-SpringBoot 特性


    笔记来源:尚硅谷雷神SpringBoot2零基础入门springboot全套完整版(spring boot2)

    参考笔记:SpringBoot2核心技术与响应式编程 · 语雀 (yuque.com)

    SpringBoot 特性

    1、依赖管理

    1.1、父项目依赖管理

    pom.xml 的父项目 spring-boot-starter-parent

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
    </parent>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    spring-boot-starter-parent 的父项目 spring-boot-dependencies

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.7.0</version>
    </parent>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    spring-boot-dependencies 中定义了很多依赖

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-amqp</artifactId>
                <version>${activemq.version}</version>
            </dependency>
            <!-- ... -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
            <!-- ... -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
            <!-- ... -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <!-- ... -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <!-- ... -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.protobuf</groupId>
                        <artifactId>protobuf-java</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- ... -->
        </dependencies>
    </dependencyManagement>
    
    • 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

    1.2、starter 场景启动器

    SpringBoot 官方支持的场景详见官方文档:

    https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters

    • spring-boot-starter-** 代表官方的某种场景
    • *-spring-boot-starter* 代表三方的某种场景

    引入某种 starter 后,该场景下所有常规所需的依赖都会被自动引入

    01-SpringBoot 特性

    所有场景启动器都有最底层的依赖 spring-boot-starter

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.7.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.3、自动版本仲裁机制

    spring-boot-dependencies 中几乎声明了所有开发中常用的依赖版本号。因此

    • 引入版本仲裁的依赖,默认都可以不写版本号
    • 引入非版本仲裁的依赖,都要写版本号
    <properties>
        <activemq.version>5.16.5</activemq.version>
        <!-- ... -->
        <aspectj.version>1.9.7</aspectj.version>
        <!-- ... -->
        <commons-pool.version>1.6</commons-pool.version>
        <!-- ... -->
        <ehcache.version>2.10.9.2</ehcache.version>
        <!-- ... -->
        <freemarker.version>2.3.31</freemarker.version>
        <!-- ... -->
        <groovy.version>3.0.10</groovy.version>
        <!-- ... -->
        <hibernate.version>5.6.9.Final</hibernate.version>
        <!-- ... -->
        <jersey.version>2.35</jersey.version>
        <!-- ... -->
        <junit.version>4.13.2</junit.version>
        <!-- ... -->
        <log4j2.version>2.17.2</log4j2.version>
        <!-- ... -->
        <mysql.version>8.0.29</mysql.version>
        <!-- ... -->
        <spring-framework.version>5.3.20</spring-framework.version>
        <!-- ... -->
        <thymeleaf.version>3.0.15.RELEASE</thymeleaf.version>
        <!-- ... -->
        <tomcat.version>9.0.63</tomcat.version>
        <!-- ... -->
    </properties>
    
    • 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
    • 可以修改版本仲裁的依赖的默认版本号
    <properties>
        <mysql.version>5.1.43</mysql.version>
    </properties>
    <dependencies>
        <!-- ... -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    刷新依赖关系,会发现 mysql-connector-java 依赖的版本号就会变成修改的版本号

    image-20220622210018213

    2、自动配置

    2.1、Tomcat

    • 自动引入 Tomcat 相关依赖
    • 配置 Tomcat
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.7.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    spring-boot-starter-tomcat 中定义了 tomcat 相关依赖

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>9.0.63</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>tomcat-annotations-api</artifactId>
                <groupId>org.apache.tomcat</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.2、SpringMVC

    • 自动引入 SpringMVC 全套组件
    • 自动配好 SpringMVC 常用组件(功能)
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.7.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    spring-boot-starter-web 中定义了 springmvc 相关依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.20</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.20</version>
        <scope>compile</scope>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.3、Web 常见功能

    SpringBoot 帮我们配置好了所有 web 开发的常见场景,如字符编码问题

    Q:如何证明?

    A:打印容器中所有的 bean

    @SpringBootApplication
    public class MainApplication
    {
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
            String[] beanDefinitionNames = context.getBeanDefinitionNames();
            for (String beanDefinitionName : beanDefinitionNames) {
                System.out.println(beanDefinitionName);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    日志信息

    org.springframework.context.annotation.internalConfigurationAnnotationProcessor
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor
    org.springframework.context.annotation.internalCommonAnnotationProcessor
    org.springframework.context.event.internalEventListenerProcessor
    org.springframework.context.event.internalEventListenerFactory
    mainApplication
    org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
    helloController
    org.springframework.boot.autoconfigure.AutoConfigurationPackages
    org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
    propertySourcesPlaceholderConfigurer
    org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration
    websocketServletWebServerCustomizer
    org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration
    org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
    tomcatServletWebServerFactory
    org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
    servletWebServerFactoryCustomizer
    tomcatServletWebServerFactoryCustomizer
    org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
    org.springframework.boot.context.internalConfigurationPropertiesBinderFactory
    org.springframework.boot.context.internalConfigurationPropertiesBinder
    org.springframework.boot.context.properties.BoundConfigurationProperties
    org.springframework.boot.context.properties.EnableConfigurationPropertiesRegistrar.methodValidationExcludeFilter
    server-org.springframework.boot.autoconfigure.web.ServerProperties
    webServerFactoryCustomizerBeanPostProcessor
    errorPageRegistrarBeanPostProcessor
    org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration
    dispatcherServlet
    spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties
    org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration
    dispatcherServletRegistration
    org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
    org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration
    taskExecutorBuilder
    applicationTaskExecutor
    spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties
    org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration
    error
    beanNameViewResolver
    org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration
    conventionErrorViewResolver
    spring.web-org.springframework.boot.autoconfigure.web.WebProperties
    org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
    errorAttributes
    basicErrorController
    errorPageCustomizer
    preserveErrorControllerTargetClassPostProcessor
    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
    requestMappingHandlerAdapter
    welcomePageHandlerMapping
    localeResolver
    themeResolver
    flashMapManager
    mvcConversionService
    mvcValidator
    mvcContentNegotiationManager
    requestMappingHandlerMapping
    mvcPatternParser
    mvcUrlPathHelper
    mvcPathMatcher
    viewControllerHandlerMapping
    beanNameHandlerMapping
    routerFunctionMapping
    resourceHandlerMapping
    mvcResourceUrlProvider
    defaultServletHandlerMapping
    handlerFunctionAdapter
    mvcUriComponentsContributor
    httpRequestHandlerAdapter
    simpleControllerHandlerAdapter
    handlerExceptionResolver
    mvcViewResolver
    mvcHandlerMappingIntrospector
    viewNameTranslator
    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
    defaultViewResolver
    viewResolver
    requestContextFilter
    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
    formContentFilter
    org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
    mbeanExporter
    objectNamingStrategy
    mbeanServer
    spring.jmx-org.springframework.boot.autoconfigure.jmx.JmxProperties
    org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration
    springApplicationAdminRegistrar
    org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration
    forceAutoProxyCreatorToUseClassProxying
    org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
    org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration
    applicationAvailability
    org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration
    standardJacksonObjectMapperBuilderCustomizer
    spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties
    org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration
    jacksonObjectMapperBuilder
    org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration
    parameterNamesModule
    org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration
    jacksonObjectMapper
    org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
    jsonComponentModule
    jsonMixinModule
    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
    org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration
    lifecycleProcessor
    spring.lifecycle-org.springframework.boot.autoconfigure.context.LifecycleProperties
    org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration
    stringHttpMessageConverter
    org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration
    mappingJackson2HttpMessageConverter
    org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration
    org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration
    messageConverters
    org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
    spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
    org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
    spring.sql.init-org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties
    org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor
    org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration
    scheduledBeanLazyInitializationExcludeFilter
    taskSchedulerBuilder
    spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
    org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
    restTemplateBuilderConfigurer
    restTemplateBuilder
    org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration
    tomcatWebServerFactoryCustomizer
    org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
    org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration
    characterEncodingFilter
    localeCharsetMappingsCustomizer
    org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
    multipartConfigElement
    multipartResolver
    spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties
    org.springframework.aop.config.internalAutoProxyCreator
    
    • 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

    可以发现其中有我们非常熟悉的一些功能

    • dispatcherServlet
    • mvcViewResolver
    • characterEncodingFilter
    • multipartResolver

    2.4、默认包扫描

    • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
    • 无需以前的包扫描配置
    • 想要改变扫描路径,@SpringBootApplication(scanBasePackages="xxx") 或者 @ComponentScan 指定扫描路径
    @SpringBootApplication
    
    • 1

    等价于

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan("xxx")
    
    • 1
    • 2
    • 3

    2.5、默认配置值

    • 默认端口号:8080

    image-20220624212901812

    • 默认配置最终都是映射到某个类上,如 MultipartProperties

    image-20220624212728818

    • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象

    image-20220624213102350

    2.6、按需加载

    • 非常多的 starter

    image-20220624213515849

    • 引入了哪些场景,哪些场景的自动配置才会开启
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • SpringBoot 所有的自动配置功能都在 spring-boot-autoconfigure 包里面

    image-20220624213822895

  • 相关阅读:
    快速灵敏的 Flink1
    STM32学习笔记一:开发环境
    Self-Supervised Exploration via Disagreement论文笔记
    Miniconda 安装和使用笔记
    sql行转列三个方法
    每日五题-202202
    文本关键信息抽取-面向复杂文本结构的实体关系联合抽取研究(论文研读)(二)
    图扑软件数字孪生厦门隧道,助力交通数字化升级
    数据集笔记:芝加哥共享单车OD数据
    报表组件DevExpress Reporting中文教程 - 如何直接在浏览器中生成报表?
  • 原文地址:https://blog.csdn.net/qq_35925558/article/details/125459714