Spring Boot 应用程序属性

2025年3月18日 | 阅读时长 4 分钟

Spring Boot 框架提供了一个内置的机制,通过一个名为 application.properties 的文件来配置应用程序。它位于 src/main/resources 文件夹内,如下图所示。

Spring Boot application properties

Spring Boot 提供了各种可以在 application.properties 文件中配置的属性。这些属性都有默认值。我们可以为 Spring Boot 应用程序设置一个或多个属性。如果需要,Spring Boot 也允许我们定义自己的属性。

application.properties 文件允许我们在不同环境中运行应用程序。总之,我们可以使用 application.properties 文件来

  • 配置 Spring Boot 框架
  • 定义我们的应用程序自定义配置属性

application.properties 示例

在上面的示例中,我们配置了应用程序名称端口。端口 8081 表示应用程序运行在端口8081

注意:以#开头的行是注释。

YAML 属性文件

Spring Boot 提供了一个名为 yml 的文件来配置属性。Yaml 文件之所以能工作,是因为Snake YAML jar 存在于类路径中。我们可以使用 application.yml 文件代替 application.properties 文件,但 Yml 文件必须存在于类路径中。

application.yml 示例

在上面的示例中,我们配置了应用程序名称端口。端口 8081 表示应用程序运行在端口8081

Spring Boot 属性类别

Spring Boot 属性有十六个类别,如下所示:

  1. 核心属性
  2. 缓存属性
  3. 邮件属性
  4. JSON 属性
  5. 数据属性
  6. 事务属性
  7. 数据迁移属性
  8. 集成属性
  9. Web 属性
  10. 模板属性
  11. 服务器属性
  12. 安全属性
  13. RSocket 属性
  14. Actuator 属性
  15. DevTools 属性
  16. 测试属性

应用程序属性表

下表列出了常用的 Spring Boot 属性:

属性默认值描述
调试false启用调试日志。
spring.application.name 用于设置应用程序名称。
spring.application.admin.enabledfalse用于启用应用程序的管理功能。
spring.config.name应用程序用于设置配置文件名。
spring.config.location 用于配置文件名。
server.port8080配置 HTTP 服务器端口
server.servlet.context-path 配置应用程序的上下文路径。
logging.file.path 配置日志文件的位置。
spring.banner.charsetUTF-8Banner 文件编码。
spring.banner.locationclasspath:banner.txt用于设置 Banner 文件位置。
logging.file 用于设置日志文件名。例如:data.log。
spring.application.index 用于设置应用程序索引。
spring.application.name 用于设置应用程序名称。
spring.application.admin.enabledfalse用于启用应用程序的管理功能。
spring.config.location 用于配置文件位置。
spring.config.name应用程序用于设置配置文件名。
spring.mail.default-encodingUTF-8用于设置默认 MimeMessage 编码。
spring.mail.host 用于设置 SMTP 服务器主机。例如:smtp.example.com。
spring.mail.password 用于设置 SMTP 服务器的登录密码。
spring.mail.port 用于设置 SMTP 服务器端口。
spring.mail.test-connectionfalse用于测试启动时邮件服务器是否可用。
spring.mail.username 用于设置 SMTP 服务器的登录用户名。
spring.main.sources 用于设置应用程序的源文件。
server.address 用于设置服务器应绑定的网络地址。
server.connection-timeout 设置连接器在关闭连接前等待另一个 HTTP 请求的时间(以毫秒为单位)。
server.context-path 用于设置应用程序的上下文路径。
server.port8080用于设置 HTTP 端口。
server.server-header 用于 Server 响应头(如果为空则不发送头信息)。
server.servlet-path/用于设置主分发 Servlet 的路径。
server.ssl.enabled 用于启用 SSL 支持。
spring.http.multipart.enabledtrue用于启用多部分上传支持。
spring.servlet.multipart.max-file-size1MB用于设置最大文件大小。
spring.mvc.async.request-timeout 设置超时时间(以毫秒为单位)。
spring.mvc.date-format 用于设置日期格式。例如:dd/MM/yyyy。
spring.mvc.locale 用于设置应用程序的区域设置。
spring.social.facebook.app-id 用于设置应用程序的 Facebook App ID。
spring.social.linkedin.app-id 用于设置应用程序的 LinkedIn App ID。
spring.social.twitter.app-id 用于设置应用程序的 Twitter App ID。
security.basic.authorize-mode角色用于设置要应用的身份验证模式。
security.basic.enabledtrue用于启用基本身份验证。
Spring.test.database.replaceany要替换的现有 DataSource 类型。
Spring.test.mockmvc.printdefaultMVC 打印选项
spring.freemaker.content-typetext/html内容类型值
server.server-header 用于服务器响应头的应用值。
spring.security.filter.dispatcher-typeasync, error, request安全过滤器链分发类型。
spring.security.filter.order-100安全过滤器链顺序。
spring.security.oauth2.client.registration.* OAuth 客户端注册。
spring.security.oauth2.client.provider.* OAuth 提供商详细信息。

下一个主题Spring Boot Starters