> 技术文档 > Spring Boot 配置文件常用配置属性详解(application.properties / application.yml)

Spring Boot 配置文件常用配置属性详解(application.properties / application.yml)


前言

Spring Boot 的一大优势就是通过简单的配置文件即可快速定制应用行为,而无需编写大量 XML 配置或 Java 代码。Spring Boot 使用 application.propertiesapplication.yml 作为核心配置文件,支持丰富的配置属性。

本文将详细介绍 Spring Boot 常用的配置属性,包括:

  1. 服务器配置
  2. 数据源配置
  3. JPA / Hibernate 配置
  4. 日志配置
  5. Thymeleaf / 模板引擎配置
  6. 安全配置(Spring Security)
  7. 缓存配置
  8. 任务调度配置
  9. 国际化配置
  10. 其他常用配置

1. 服务器相关配置(Server Properties)

控制嵌入式服务器(如 Tomcat、Jetty)的行为。

application.properties 示例

server.port=8080server.servlet.context-path=/apiserver.tomcat.max-connections=10000server.tomcat.max-http-form-post-size=20MBserver.error.whitelabel.enabled=false

application.yml 示例:

server: port: 8080 servlet: context-path: /api tomcat: max-connections: 10000 max-http-form-post-size: 20MB error: whitelabel: enabled: false

常见配置说明:

属性名 说明 server.port 应用监听的端口,默认 8080 server.servlet.context-path 应用的上下文路径,默认为空 server.tomcat.max-connections Tomcat 最大连接数 server.tomcat.max-http-form-post-size HTTP 表单 POST 最大大小 server.error.whitelabel.enabled 是否启用默认错误页面(关闭后返回 JSON 错误信息)

2. 数据源配置(DataSource Properties)

用于配置数据库连接池,常见如 HikariCP、Tomcat JDBC、DBCP2 等。

application.properties 示例:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTCspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.hikari.maximum-pool-size=10spring.datasource.hikari.idle-timeout=30000

application.yml 示例:

spring: datasource: url: jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver hikari: maximum-pool-size: 10 idle-timeout: 30000

常见配置说明:

属性名 说明 spring.datasource.url 数据库连接 URL spring.datasource.username 数据库用户名 spring.datasource.password 数据库密码 spring.datasource.driver-class-name 数据库驱动类名 spring.datasource.hikari.* HikariCP 特定配置(如最大连接数、空闲超时)

3. JPA / Hibernate 配置(Spring Data JPA)

用于配置 JPA 和 Hibernate 的行为。

application.properties 示例:

spring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=truespring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialectspring.jpa.open-in-view=false

application.yml 示例:

spring: jpa: hibernate: ddl-auto: update show-sql: true properties: hibernate: format_sql: true dialect: org.hibernate.dialect.MySQL8Dialect open-in-view: false

常见配置说明:

属性名 说明 spring.jpa.hibernate.ddl-auto 自动建表策略(create、update、validate、none) spring.jpa.show-sql 是否打印 SQL spring.jpa.properties.hibernate.format_sql 格式化 SQL spring.jpa.properties.hibernate.dialect Hibernate 方言 spring.jpa.open-in-view 是否启用 OpenEntityManagerInViewFilter(不推荐开启)

4. 日志配置(Logging)

Spring Boot 支持 Logback、Log4j2、Java Util Logging 等日志框架。

application.properties 示例:

logging.level.root=INFOlogging.level.com.example.demo=DEBUGlogging.file.name=logs/app.loglogging.file.max-size=10MBlogging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

application.yml 示例:

logging: level: root: INFO com.example.demo: DEBUG file: name: logs/app.log max-size: 10MB pattern: console: \"%d{yyyy-MM-dd HH:mm:ss} - %msg%n\"

常见配置说明:

属性名 说明 logging.level.* 设置不同包的日志级别 logging.file.name 日志输出文件路径 logging.file.max-size 日志文件最大大小 logging.pattern.console 控制台日志输出格式

5. 模板引擎配置(Thymeleaf)

如果你使用 Thymeleaf 模板引擎,可以配置缓存、模板路径等。

application.properties 示例:

spring.thymeleaf.cache=falsespring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTMLspring.thymeleaf.encoding=UTF-8

application.yml 示例:

spring: thymeleaf: cache: false prefix: classpath:/templates/ suffix: .html mode: HTML encoding: UTF-8

6. 安全配置(Spring Security)

用于配置 Spring Security 的默认行为。

application.properties 示例:

spring.security.user.name=adminspring.security.user.password=123456spring.security.user.roles=USER,ADMIN

application.yml 示例:

spring: security: user: name: admin password: 123456 roles: - USER - ADMIN

注意:实际项目中建议使用数据库认证,而不是配置文件方式。


7. 缓存配置(Cache)

Spring Boot 支持多种缓存实现,如 Caffeine、EhCache、Redis 等。

application.properties 示例:

spring.cache.type=simplespring.cache.cache-names=myCachespring.cache.simple.initial-capacity=100spring.cache.simple.max-entries=500

application.yml 示例:

spring: cache: type: simple cache-names: myCache simple: initial-capacity: 100 max-entries: 500

8. 任务调度配置(Scheduling)

启用定时任务并配置线程池。

application.properties 示例:

spring.task.scheduling.pool.size=5

application.yml 示例:

spring: task: scheduling: pool: size: 5

在代码中使用 @Scheduled 注解即可定义定时任务。


9. 国际化配置(i18n)

配置消息源和默认语言。

application.properties 示例:

spring.messages.basename=messagesspring.messages.encoding=UTF-8spring.locale=zh

application.yml 示例:

spring: messages: basename: messages encoding: UTF-8 locale: zh

10. 其他常用配置

10.1 文件上传配置

spring.servlet.multipart.max-file-size=10MBspring.servlet.multipart.max-request-size=10MB
spring: servlet: multipart: max-file-size: 10MB max-request-size: 10MB

10.2 WebMvc 配置

spring.mvc.async.request-timeout=0spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss
spring: mvc: async: request-timeout: 0 format: date-time: yyyy-MM-dd HH:mm:ss

10.3 Actuator 配置

management.endpoints.web.exposure.include=*management.endpoint.health.show-details=always
management: endpoints: web: exposure: include: \"*\" endpoint: health: show-details: always

总结

Spring Boot 的配置文件非常灵活,通过 application.propertiesapplication.yml 可以快速配置服务器、数据库、日志、缓存、安全等多个模块的行为。使用合适的配置,可以显著提升开发效率和系统稳定性。

建议:在开发阶段启用更多调试信息(如 SQL 打印),在生产环境中关闭调试输出并启用缓存、日志分割等优化配置。