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 示例:

properties 复制代码
server.port=8080
server.servlet.context-path=/api
server.tomcat.max-connections=10000
server.tomcat.max-http-form-post-size=20MB
server.error.whitelabel.enabled=false

application.yml 示例:

yaml 复制代码
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 示例:

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

application.yml 示例:

yaml 复制代码
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 示例:

properties 复制代码
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.open-in-view=false

application.yml 示例:

yaml 复制代码
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 示例:

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

application.yml 示例:

yaml 复制代码
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 示例:

properties 复制代码
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8

application.yml 示例:

yaml 复制代码
spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML
    encoding: UTF-8

6. 安全配置(Spring Security)

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

application.properties 示例:

properties 复制代码
spring.security.user.name=admin
spring.security.user.password=123456
spring.security.user.roles=USER,ADMIN

application.yml 示例:

yaml 复制代码
spring:
  security:
    user:
      name: admin
      password: 123456
      roles:
        - USER
        - ADMIN

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


7. 缓存配置(Cache)

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

application.properties 示例:

properties 复制代码
spring.cache.type=simple
spring.cache.cache-names=myCache
spring.cache.simple.initial-capacity=100
spring.cache.simple.max-entries=500

application.yml 示例:

yaml 复制代码
spring:
  cache:
    type: simple
    cache-names: myCache
    simple:
      initial-capacity: 100
      max-entries: 500

8. 任务调度配置(Scheduling)

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

application.properties 示例:

properties 复制代码
spring.task.scheduling.pool.size=5

application.yml 示例:

yaml 复制代码
spring:
  task:
    scheduling:
      pool:
        size: 5

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


9. 国际化配置(i18n)

配置消息源和默认语言。

application.properties 示例:

properties 复制代码
spring.messages.basename=messages
spring.messages.encoding=UTF-8
spring.locale=zh

application.yml 示例:

yaml 复制代码
spring:
  messages:
    basename: messages
    encoding: UTF-8
  locale: zh

10. 其他常用配置

10.1 文件上传配置

properties 复制代码
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
yaml 复制代码
spring:
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB

10.2 WebMvc 配置

properties 复制代码
spring.mvc.async.request-timeout=0
spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss
yaml 复制代码
spring:
  mvc:
    async:
      request-timeout: 0
    format:
      date-time: yyyy-MM-dd HH:mm:ss

10.3 Actuator 配置

properties 复制代码
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
yaml 复制代码
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

总结

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

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

相关推荐
旷野说1 分钟前
为什么 MyBatis 原生二级缓存“难以修复”?
java·java-ee·mybatis
8***23554 分钟前
【wiki知识库】07.用户管理后端SpringBoot部分
java
bcbnb6 分钟前
iOS 性能测试的工程化方法,构建从底层诊断到真机监控的多工具测试体系
后端
开心就好20259 分钟前
iOS 上架 TestFlight 的真实流程复盘 从构建、上传到审核的团队协作方式
后端
小周在成长17 分钟前
Java 泛型支持的类型
后端
aiopencode17 分钟前
Charles 抓不到包怎么办?HTTPS 抓包失败、TCP 数据流异常与底层补抓方案全解析
后端
阿蔹20 分钟前
JavaWeb-Selenium 配置以及Selenim classnotfound问题解决
java·软件测试·python·selenium·测试工具·自动化
稚辉君.MCA_P8_Java22 分钟前
Gemini永久会员 C++返回最长有效子串长度
开发语言·数据结构·c++·后端·算法
Penge66641 分钟前
Redis-bgsave浅析
redis·后端
阿白的白日梦1 小时前
Windows下c/c++编译器MinGW-w64下载和安装
c语言·后端