SpringBoot_第三章(依赖管理)

目录

1:SpringBoot的依赖管理

1.1:依赖管理结构

2:父依赖spring-boot-starter-perent

2.1:父级标签

2.2:properties标签

2.3:resource标签

2.4:plugin插件信息

3:祖宗依赖spring-boot-dependencies

4:为什么不需要Tomcat

5:springboot的starter(启动器)

5.1:web项目的spring-boot-starter-web

5.2:需要发送邮件的Mail Starter

6:依赖管理总结


1:SpringBoot的依赖管理

在springboot项目中,简化了Spring应用的初始搭建以及开发过程,同样对jar的依赖管理也有一套自己的标准。我们创建好自己的springboot项目模板后可以看到pom文件,对需要的jar进行依赖管理,在将项目打包之后,由于项目包包含Tomcat,因此我们可以直接使用java -jar 项目包来运行项目。这些都是依赖于springboot的项目管理,下边来具体分析。

1.1:依赖管理结构

我们创建好项目,查看pom文件如下开始分析

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--springBoot父类启动器-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--本项目信息-->
    <groupId>com.example</groupId>
    <artifactId>SpringBoot2_01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot2_01</name>
    <description>SpringBoot2_01</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!--springWeb依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--  引入其他的springboot官网starter 自动导入相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <!--热部署,改代码不需要重启-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- lomBok注解依赖包-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 测试类依赖包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- mysql依赖jar-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- springboot打包插件 打成一个胖包  包含所有的东西 直接运行-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.project-lombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

分析结构:

我们在此pom中可以看到项目的父级依赖是spring-boot-starter-perent,

我们点击进入spring-boot-starter-perent

看到他还有一个父级依赖是spring-boot-dependencies 

                                 他们结构是

  -------本项目pom.xml

        ---------spring-boot-starter-perent

                   ------spring-boot-dependencies 

2:父依赖spring-boot-starter-perent

我们查看spring-boot-starter-perent,由于此过长。我们查看有效信息得知spring-boot-starter-perent没用定义jar管理的dependencyManagement标签。

它里边主要有四大标签

2.1:父级标签

这里边有匹配当前的springboot版本的依赖其他jar包的版本,便于我们直接引入相应的jar,不需要指定版本。对于没有指定的jar还是需要自己引入指定版本的。

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

2.2:properties标签

此标签的作用是指定了Java的编译版本,编译编码的信息

 <properties>
    <java.version>1.8</java.version>
    <resource.delimiter>@</resource.delimiter>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

2.3:resource标签

此标签的作用是制定了springboot项目的配置文件在resource下边或者其他位置。可以使yml、ymal、properties三种格式的文件

<resources>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.yaml</exclude>
          <exclude>**/application*.properties</exclude>
        </excludes>
      </resource>
    </resources>

2.4:plugin插件信息

该插件可以使maven直接将依赖的jar打包,可以使用java -jar跑项目了。

 <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <executions>
            <execution>
              <id>repackage</id>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <mainClass>${start-class}</mainClass>
          </configuration>
        </plugin>

3:祖宗依赖spring-boot-dependencies

这个依赖的pom很长。这里边有各种各样的springboot依赖。主要是管理springboot不同版本对应的jar。我们可以在自己的项目中直接使用,不需要指定版本。

<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>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-broker</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-camel</artifactId>
        <version>${activemq.version}</version>
      </dependency>

        此处忽略很多,只是作为演示

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.6.7</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.6.7</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <version>2.6.7</version>
      </dependency>
</dependencyManagement>

我们可以在自己的项目中直接使用,或者覆盖掉springboot的依赖版本管理

         <!--springboot的web依赖 无需指定版本-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 直接使用springboot的依赖管理 或者自己加上版本号 覆盖掉springboot的版本-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

4:为什么不需要Tomcat

当我们需要web模块的时候,只要引入spring-boot-starter-web

 <!--springboot的web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

这个jar的pom中我们查看此pom中有各种依赖,依赖图可以看到spring依赖 tomcat依赖 json依赖等。 所以初步判定打包的项目中tomcat。

5:springboot的starter(启动器)

springboot的starter是各种场景启动器。我们需要使用什么功能就配置什么starter。对应的starter中有此功能支持的各种jar,官网文档有很多的启动器,不需要我们在去找对应的jar和处理版本兼容了。规范是spring-boot-starter-*

 

5.1:web项目的spring-boot-starter-web

比如web的starter。我们只需要引入jar包,就有了上边的tomcat,spring的jar和mvc的jar。就可以使用注解开发web项目了。

 

5.2:需要发送邮件的Mail Starter

我们只需要导入此jar,然后写业务代码就行了。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

6:依赖管理总结

所谓的依赖管理就是把各种启动器导进来,启动器会把需要的jar包和版本自动导入,开发的时候不需要自己导入jar和解决依赖冲突了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值