springboot中的pom文件是如何管理依赖的

我们来看一下新建完成后的springboot中的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.1.6.RELEASE</version>
          <relativePath/> <!-- lookup parent from repository -->
      </parent>
      <groupId>com.guomie</groupId>
      <artifactId>spring-boot-ymal</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>spring-boot-ymal</name>
      <description>Demo project for Spring Boot</description><properties>
          <java.version>1.8</java.version>
      </properties><dependencies>
          <!--web依赖-->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
    </dependencies>
  </project>

 

我们主要看一下他的父依赖

  <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.1.6.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
   <!--父类的父类 -->
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.1.6.RELEASE</version>
      <relativePath>../../spring-boot-dependencies</relativePath>
    </parent>

 

这里我们可以看到他里面有些写版本的依赖和版本号

依赖配置都声明在了dependencyManagement 里面,所以他的子项目不会自动注入这些依赖,只能通过自项目显示注入自己需要的依赖,默认用的就是父依赖中的版本号,所以我们在注入这种starter的时候不需要指定版本号。

我们再看一下我们引入的starter

这里我们以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.1.6.RELEASE</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-json</artifactId>
        <version>2.1.6.RELEASE</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.1.6.RELEASE</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.17.Final</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.1.8.RELEASE</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.8.RELEASE</version>
        <scope>compile</scope>
      </dependency>
    </dependencies>

所以springboot帮我们管理好了所需要依赖的版本号,而且按照模块去封装成一个个starter,我们在使用的时候就很更简单快捷。

猜你喜欢

转载自www.cnblogs.com/guomie/p/11261345.html