全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-18 8 分钟 ✍️ juanwangdev

SpringBoot application.yml配置详解

application.yml 是 SpringBoot 主配置文件。

基础结构

YAML
# 服务器配置
server:
  port: 8080
  servlet:
    context-path: /

# Spring配置
spring:
  application:
    name: my-app

# 日志配置
logging:
  level:
    root: info

server配置

YAML
server:
  port: 8080                    # 端口
  servlet:
    context-path: /app          # 上下文路径
    encoding:
      charset: UTF-8            # 编码
      enabled: true
      force: true
  error:
    path: /error                # 错误页面路径
  tomcat:
    max-connections: 10000      # 最大连接
    threads:
      max: 200                  # 最大线程

spring配置

YAML
spring:
  # 应用名称
  application:
    name: my-app

  # Profile配置
  profiles:
    active: dev                 # 激活环境

  # 数据源配置
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    hikari:
      maximum-pool-size: 20
      minimum-idle: 5

  # JPA配置
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true

  # Redis配置
  data:
    redis:
      host: localhost
      port: 6379
      password: secret
      database: 0

  # MVC配置
  mvc:
    view:
      prefix: /templates/
      suffix: .html
    static-path-pattern: /static/**

  # 静态资源
  web:
    resources:
      static-locations:
        - classpath:/static/
        - classpath:/public/

  # 文件上传
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 100MB

  # JSON配置
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: non_null

logging配置

YAML
logging:
  level:
    root: info                  # 根日志级别
    com.example: debug          # 包级别
    org.springframework: warn   # 框架级别
  file:
    name: logs/app.log          # 日志文件
    max-size: 10MB              # 最大大小
    max-history: 30             # 保留天数
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger - %msg%n"
    file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger - %msg%n"

日志级别说明

级别用途
trace详细调试信息
debug调试信息
info运行信息
warn警告信息
error错误信息

actuator配置

YAML
management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics  # 暴露端点
      base-path: /actuator
  endpoint:
    health:
      show-details: always            # 显示详情
  health:
    db:
      enabled: true
    redis:
      enabled: true

自定义配置

YAML
# 自定义业务配置
app:
  name: my-application
  version: 1.0.0
  timeout: 3000
  features:
    cache-enabled: true
    auth-required: true
Java
// 读取自定义配置
@Value("${app.name}")
private String appName;

@Value("${app.timeout:5000}")    // 默认值5000
private int timeout;

完整配置示例

YAML
server:
  port: 8080
  servlet:
    context-path: /
    encoding:
      charset: UTF-8
      enabled: true

spring:
  application:
    name: my-app
  profiles:
    active: dev
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: false
  servlet:
    multipart:
      max-file-size: 10MB

logging:
  level:
    root: info
    com.example: debug
  file:
    name: logs/app.log

app:
  name: my-app
  timeout: 3000

要点总结

  • server.port配置端口
  • spring.datasource配置数据库
  • logging.level配置日志级别
  • 自定义配置用@Value读取
  • 合理配置减少硬编码

📝 发现内容有误?点击此处直接编辑

← 上一篇 SpringBoot Maven与Gradle构建工具
下一篇 → SpringBoot命令行运行JAR
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库