(三)Spring Boot 官网文档学习之默认配置

本文介绍如何在Spring Boot项目中使用spring-boot-starter-parent来获得默认配置,并通过实例演示如何覆盖这些默认设置。同时,文章还解释了启动器的概念及其在引入Spring Framework功能时的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


原文地址:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#using-boot


继承 spring-boot-starter-parent

当我们在 pom 里面继承 spring-boot-starter-parent 的时候,我们将自动的获得一些默认值,这些默认值都是 spring-boot-starter-parent 里面默认配置的;

一般都有下面默认值:

  • JDK8 将作为默认的编译器;
  • 所有的编码,都将自动的使用 UTF8

还有一些其他默认值,博主初学,暂时也不明白是什么意思,贴出原文,大家自己看看:

A Dependency Management section, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit <version> tags for those dependencies when used in your own pom.

An execution of the repackage goal with a repackage execution id. Sensible resource filtering.

Sensible plugin configuration (exec plugin, Git commit ID, and shade).

Sensible resource filtering for application.properties and application.yml including profile-specific files (for example, application-dev.properties and application-dev.yml)

Note that, since the application.properties and application.yml files accept Spring style placeholders (${…​}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)


覆盖默认配置

继承默认配置以后,还是可以覆盖掉默认配置的;

我们只需要在 <properties> 里面配置,我们想要覆盖的属性的具体版本,即可:

例如,覆盖 spring-data-releasetrain 的默认版本:

<properties>
	<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

和上一篇,使用没有 <parent>pom,而使用 spring-boot-dependencies 一样,都可以覆盖默认配置;


启动器

启动器,可以理解为一组配置,被打包成一个依赖包了;

比如我们需要 Spring Framework 框架的缓存功能,则可以在 <dependencies> 里面添加 spring-boot-starter-cache 启动器 依赖;

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

官方的启动器名字格式都是 spring-boot-starter-* ,在 IDEA 配置好 Maven 以后,可以使用 Ctrl + 空格键 进行智能提示;

官网给出了启动器的列表,以及说明,地址 :Spring Boot 启动器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值