SpringBoot的两大特性

SpringBoot的两大特性

  1. 依赖管理
    • 依赖管理

      在SpringBoot的开始现在pom.xml中添加了一个父项目

      <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.3.4.RELEASE</version>
         </parent>
      

      这个父项目还有一个父项目,它的父项目是

      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-dependencies</artifactId>
          <version>2.3.4.RELEASE</version>
        </parent>
      

      点开这个父项目可以看到里面约束了各种依赖的版本

      之所以在引入依赖的时候,没有指明版本号,就是因为其可以自动继承父项目中的版本号,几乎声明了常用的jar的版本号

    • 如果不想使用父项目中规定的版本号,也可以自定义版本号

      1. 查看spring-boot-dependencies里面规定当前依赖版本用的key,即是版本号

      2. 在当前项目里面重写配置

        <properties>
        		<mysql.version>5.1.43</mysql.version>
        </properties>
        
    • 开发期间的各种starters

      官方地址

      https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
      
      1. 见到很多spring-boot-starters-*:*就某种场景

      2. 只要引入starters,这个场景的所有常规需要的依赖我们都会自动引入

      3. SpringBoot所有支持的场景都可以在上方官网中找到

      4. 如果见到了thirdpartyproject-spring-boot-starter开头的starters,一般都是第三方为我们提供的简化开发的场景启动器

      5. 引入web依赖

        <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
        
      6. 所有场景启动器依赖最底层的依赖都会依赖下面这个依赖

            <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
                  <version>2.3.4.RELEASE</version>
                  <scope>compile</scope>
            </dependency>
        
    • 无需写版本号,自动版本仲裁

      1. 引入依赖默认都可以不写版本号
      2. 引入的非版本仲裁的jar一定要写版本号,就是在父项目中没有指定的jar需要写
  2. 自动配置
    • 自动配置TomCat

      • 引入TomCat依赖

        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <version>2.3.4.RELEASE</version>
                <scope>compile</scope>
        </dependency>
        
      • 配置TomCat

    • 自动配置好SpringMVC

      • 引入了SpringMVC开发的全部组件

        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.2.9.RELEASE</version>
                <scope>compile</scope>
        </dependency>
        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.9.RELEASE</version>
                <scope>compile</scope>
        </dependency>
        
      • 自动配好了SpringMVC常用组件(功能)

      • 可以在主程序中编写代码查看SpringBoot为我们加载了多少组件

        public static void main(String[] args) {
                //1、返回IOC容器
                ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
                //2、查看容器中的组件
                String[] names = run.getBeanDefinitionNames();
                //3、打印出来
                for (String name : names){
                    System.out.println(name);
                }
        

在这里插入图片描述

 - SpringBoot已经为我们配置好了所有web开发的常见场景
  • 默认的包结构

    • 主程序所在的包以及下面的所有子包里面的组件都会被默认扫描进来

    • 无需配置以前的包扫描

    • 如果想要变动包结构,则可能出现扫描不到的问题,如果非要变动包结构,就需要更改扫描的基础包位置,在主程序上的注解@SpringBootApplication中有一个属性是scanBasePackages来设置扫描基础包,默认扫描的基础包是主程序所在的包

      b@SpringBootApplication(scanBasePackages = "com.gis507")
      

      除此之外,还有一个包注解@ComponentScan

      还可以将@SpringBootApplication分解

      @SpringBootConfiguration
      @EnableAutoConfiguration
      @ComponentScan("com.gis507")
      

      @SpringBootApplication的源码

      @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Inherited
      @SpringBootConfiguration
      @EnableAutoConfiguration
      @ComponentScan(
              excludeFilters = {@Filter(
              type = FilterType.CUSTOM,
              classes = {TypeExcludeFilter.class}
      ), @Filter(
              type = FilterType.CUSTOM,
              classes = {AutoConfigurationExcludeFilter.class}
      )}
      )
      public @interface SpringBootApplication {
      }
      

      就会扫描com.gis507包以及子包下所有的组件

  • 各种配置具有默认值

    • 默认配置最后都是映射到某一个类上的
    • 配置文件上的值最后都会绑定到每个类上,这个类会在容器中创建对象
  • 按需加载所有的自动配置项

    • 非常多的starters
    • 引入了哪些场景(starter),就会引入哪些配置
    • SpringBoot所有的自动配置功能都在spring-boot-autoconfigure包里面
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值