欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

spring-boot-starter-parent 作用

发布时间:2025/3/19 编程问答 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 spring-boot-starter-parent 作用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一、你的项目 pom.xml 中有这段代码吗

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

new project > spring initializr 创建一个 Spring Boot 项目时:

或者是干脆从 https://start.spring.io/ 在线生成 Spring Boot 项目:

我们都会发现spring-boot-starter-parent的身影,那么他到底是干啥的呢

这是 Spring Boot 的父级依赖,这样当前的项目就是 Spring Boot 项目了。spring-boot-starter-parent 是一个特殊的 starter ,它用来提供相关的 Maven 默认依赖。

使用它之后,常用的包依赖可以省去 version 标签,当我们搭建web应用的时候,可以像下面这样添加spring-boot-starter-web依赖:

<!-- Quartz定时任务 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId> </dependency>

二、这个parent还提供了哪些特性呢

注意,关于 parent 提供的一些特性都是依据 maven 环境的,可以说
spring-boot-starter-parent 是 maven 独有的,如下是提供的一些特性:

  • 默认使用Java8,可添加以下配置修改版本:
  • <properties><java.version>1.8</java.version> </properties>
  • 默认使用UTF-8编码,可添加以下配置修改编码:
  • <properties><project.build.sourceEncoding>GBK</project.build.sourceEncoding> </properties>
  • 省略version信息
    在 dependencies 里的部分配置可以不用填写 version 信息,这些 version 信息会从 spring-boot-dependencies 里得到继承。
  • <!-- JPA --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><version>2.1.8</version> </dependency>

    使用 spring-boot-starter-parent 的话,可以这样,继承默认版本:

    <!-- JPA --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

    正常来说项目应该添加了以下带版本的依赖:
    4. 识别过来资源过滤
    例如,打包的时候把 src/main/resources 下所有文件都打包到包中。

    <resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes><filtering>true</filtering> </resource>
  • 识别插件的配置
  • 比如 exec plugin, surefire, Git commit ID, shade

    能够识别 application.properties 和 application.yml 类型的文件,同时也能支持 profile-specific 类型的文件(如: application-foo.properties and application-foo.yml,这个功能可以更好的配置不同生产环境下的配置文件)。

    三、覆盖并使用使用自己的依赖版本

    使用 spring-boot-starter-parent 来帮我们管理 version 确实方便了不少,但是往往我们需要自己去配置某些版本,怎么办呢?

    我们可以通过覆盖 properties 标签的 property 标签来达到修改依赖版本号的目的,例如上方的修改默认的编码方式、以及默认jdk版本:

    <properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

    为什么这样可以实现?

    我们了解 Spring Boot Dependencies 定义了所有 Spring Boot 项目的默认依赖关系管理。如果我们想要使用特定依赖项的新版本,我们可以通过在项目 pom 中指定新属性来覆盖该版本。

    下面的摘录显示了由 Spring Boot Dependencies 父 pom 管理的一些重要依赖项。由于 Spring Boot Starter Parent 继承自 spring-boot-dependencies,因此它也共享所有这些特性。

    <properties><activemq.version>5.13.4</activemq.version>...<ehcache.version>2.10.2.2.21</ehcache.version><ehcache3.version>3.1.1</ehcache3.version>...<h2.version>1.4.192</h2.version><hamcrest.version>1.3</hamcrest.version><hazelcast.version>3.6.4</hazelcast.version><hibernate.version>5.0.9.Final</hibernate.version><hibernate-validator.version>5.2.4.Final</hibernate-validator.version><hikaricp.version>2.4.7</hikaricp.version><hikaricp-java6.version>2.3.13</hikaricp-java6.version><hornetq.version>2.4.7.Final</hornetq.version><hsqldb.version>2.3.3</hsqldb.version><htmlunit.version>2.21</htmlunit.version><httpasyncclient.version>4.1.2</httpasyncclient.version><httpclient.version>4.5.2</httpclient.version><httpcore.version>4.4.5</httpcore.version><infinispan.version>8.2.2.Final</infinispan.version><jackson.version>2.8.1</jackson.version>....<jersey.version>2.23.1</jersey.version><jest.version>2.0.3</jest.version><jetty.version>9.3.11.v20160721</jetty.version><jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version><spring-security.version>4.1.1.RELEASE</spring-security.version><tomcat.version>8.5.4</tomcat.version><undertow.version>1.3.23.Final</undertow.version><velocity.version>1.7</velocity.version><velocity-tools.version>2.0</velocity-tools.version><webjars-hal-browser.version>9f96c74</webjars-hal-browser.version><webjars-locator.version>0.32</webjars-locator.version><wsdl4j.version>1.6.3</wsdl4j.version><xml-apis.version>1.4.01</xml-apis.version> </properties>

    所以,我们更改了 property 后,也实现了我们的需求,当然我们还有直接注明 version 的方式呢,如下:

    <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version> </dependency>

    四、一个完整的pom.xml实例

    <?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><groupId>club.sscai.tmax</groupId><artifactId>tmax</artifactId><version>0.0.1-SNAPSHOT</version><name>tmax</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!-- Mysql Connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!-- JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version></dependency><!-- Swagger API文档 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><!-- 热更新 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

    总结

    以上是生活随笔为你收集整理的spring-boot-starter-parent 作用的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。