2、SpringBoot_依赖介绍

三、SpringBoot介绍

1.parent

  • 前言:之前是使用spring/springmvc 开发,整合不同的组件会有很多依赖,这些依赖会涉及到很多的版本信息,版本信息多了之后可能会导致版本冲突问题
  • 概述:把很多组件技术的搭配放到一起,名字就叫 parent

1.1实际开发中会引入 parent

  • 引入parent

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  • 点击后发现又引入了一个 parent

    • 通过该parent 能够找到对应的搭配
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.7.5</version>
    </parent>
    
  • 通过propertie管理版本信息

    <properties>
        <activemq.version>5.16.5</activemq.version>
        <antlr2.version>2.7.7</antlr2.version>
        <appengine-sdk.version>1.9.98</appengine-sdk.version>
        <artemis.version>2.19.1</artemis.version>
        <aspectj.version>1.9.7</aspectj.version>
        <assertj.version>3.22.0</assertj.version>
        <atomikos.version>4.0.6</atomikos.version>
        <awaitility.version>4.2.0</awaitility.version>
        <build-helper-maven-plugin.version>3.3.0</build-helper-maven-plugin.version>
        <byte-buddy.version>1.12.18</byte-buddy.version>
        <cache2k.version>2.6.1.Final</cache2k.version>
    </properties>
    
  • 通过dependencyManagement去管理依赖,子模块想要使用需要去引用依赖才可以,该标签不强制使用依赖需要时才导入

    <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-amqp</artifactId>
            <version>${activemq.version}</version>
          </dependency>
          <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-blueprint</artifactId>
            <version>${activemq.version}</version>
          </dependency>
    </dependencyManagement>
    

1.2引入依赖

  • 通过 dependencies

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  • 通过导入方式获取依赖(使用alibaba的方式创建的

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    </dependencyManagement>
    

2.starter

  • 回顾springmvc
    • 问题:之前的操作导入依赖比较麻烦,比如我们要使用Spring-webmvc 一定会去使用Spring-web
    • 引入太繁琐不好管理
  • starter:springboot团队直接把这些固定搭配技术做成一个成品

2.1实际开发体验

  • 开发springmvc(引入此依赖就解决了springmvc的一系列引用,内部引用了多个web开发需要的依赖,就可以直接进行web的开发

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  • 点击引入的依赖会有如下依赖

    <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <version>2.7.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-json</artifactId>
          <version>2.7.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <version>2.7.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.3.23</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.23</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
    
  • 如何引入json操作

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-json</artifactId>
        <version>2.7.5</version>
        <scope>compile</scope>
    </dependency>
    
  • 点开又会找到曾经我们导入的依赖

    <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <version>2.7.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.3.23</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.13.4.2</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.datatype</groupId>
          <artifactId>jackson-datatype-jdk8</artifactId>
          <version>2.13.4</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.datatype</groupId>
          <artifactId>jackson-datatype-jsr310</artifactId>
          <version>2.13.4</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.module</groupId>
          <artifactId>jackson-module-parameter-names</artifactId>
          <version>2.13.4</version>
          <scope>compile</scope>
        </dependency>
    </dependencies>
    

2.2starter总结

  • starter 其实就是很多个坐标的组合

  • 以后引入了什么坐标,直接通过maven观察即可

    2、SpringBoot_依赖介绍_第1张图片

2.3parent和starter总结

  • parent作用:
    • 定义若干版本信息,交给springboot统一管理,目的是减少依赖冲突
  • starter作用
    • 在同一个坐标中定义多个坐标,目的是简化依赖配置
  • 使用步骤
    • 先去找有没有starter ,如果有直接引用
    • 没有直接提供版本信息即可

3.引导类

3.1传统spring配置

  • 提供如下配置
    • SpringConfig
    • SpringMvcConfig

3.2引导类作用

  • SpringBoot工程提供引导类用来启动程序

  • SpringBoot工程启动后创建并初始化Spring容器

    • 证明创建了Spring容器

      @SpringBootApplication
      public class FourSpringbootApplication {
          public static void main(String[] args) {
              ConfigurableApplicationContext context = SpringApplication.run(FourSpringbootApplication.class, args);
              TestController bean = context.getBean(TestController.class);
              System.out.println(bean.test04());
          }
      }
      
  • 注意:引导类必须有一个注解@SpringBootApplication,否则启动不起来

4.内嵌tomcat

4.1定义依赖

  • 依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  • 点开发现如下依赖

    <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <version>2.7.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-json</artifactId>
          <version>2.7.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <version>2.7.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.3.23</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.23</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
    
  • 点击spring-boot-starter-tomcat

    <dependencies>
        <dependency>
          <groupId>jakarta.annotation</groupId>
          <artifactId>jakarta.annotation-api</artifactId>
          <version>1.3.5</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-core</artifactId>
          <version>9.0.68</version>
          <scope>compile</scope>
          <exclusions>
            <exclusion>
              <artifactId>tomcat-annotations-api</artifactId>
              <groupId>org.apache.tomcat</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-el</artifactId>
          <version>9.0.68</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-websocket</artifactId>
          <version>9.0.68</version>
          <scope>compile</scope>
          <exclusions>
            <exclusion>
              <artifactId>tomcat-annotations-api</artifactId>
              <groupId>org.apache.tomcat</groupId>
            </exclusion>
          </exclusions>
        </dependency>
      </dependencies>
    

4.2Tomcat运行原理

  • tomcat:是java语言开发的软件,就可以通过直接创建tomcat对象的方式去运行tomcat

  • 如何交给spring管理的

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-core</artifactId>
      <version>9.0.68</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <artifactId>tomcat-annotations-api</artifactId>
          <groupId>org.apache.tomcat</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    
  • 通过 starter引入 tomcat 服务器之后就可以直接创建tomcat对象了

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.7.5</version>
      <scope>compile</scope>
    </dependency>
    

4.3更换内置服务器

  • tomcat(默认):apache公司的,受众大,使用多,提供了较多组件

  • jetty:轻量级服务器,负载比tomcat弱

  • undertow

  • 更换内置服务器操作

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </dependency>
        </dependencies>
    

4.4总结

  • tomcat 只是springboot中的一个依赖而已
  • 想要什么服务器,直接导入对应的starter包即可

你可能感兴趣的:(spring,boot,后端,java)