application.properties配置文件详解
在 Spring Boot 中,application.properties
(或 application.yml
)是核心配置文件,用于定义应用运行时的各种参数。其配置项覆盖应用核心、数据源、Web、安全、缓存、日志、中间件集成 等多个领域。以下按功能模块分类列举常见配置项(基于 Spring Boot 3.x,部分配置可能因版本略有差异):
一、应用核心配置 控制应用的基础行为,如应用名称、端口、环境等。
配置项 说明 示例值 spring.application.name
应用名称(用于服务发现、日志标识等) my-app
server.port
Web 服务器端口(默认 8080) 8081
server.address
绑定的网卡地址(默认 0.0.0.0
,即所有地址) 127.0.0.1
server.servlet.context-path
Web 应用上下文路径(根路径) /api
spring.profiles.active
激活的环境配置(如 dev
、prod
) prod
spring.config.import
导入外部配置文件(支持 classpath:
、file:
等前缀) classpath:db-config.properties
spring.main.allow-bean-definition-overriding
是否允许 Bean 定义覆盖(默认 false
,避免冲突) true
spring.main.lazy-initialization
是否启用全局懒初始化(默认 false
,启动时不初始化 Bean) true
二、数据源与 ORM 配置 用于配置数据库连接、连接池、ORM 框架(如 JPA、MyBatis)等。
1. 通用数据源配置 配置项 说明 示例值 spring.datasource.url
数据库连接 URL jdbc:mysql://localhost:3306/mydb?useSSL=false
spring.datasource.driver-class-name
数据库驱动类名(自动检测时可省略) com.mysql.cj.jdbc.Driver
spring.datasource.username
数据库用户名 root
spring.datasource.password
数据库密码 123456
spring.datasource.hikari.*
HikariCP 连接池配置(Spring Boot 默认连接池) 见下文 Hikari 专用配置
2. HikariCP 连接池专用配置 配置项 说明 示例值 spring.datasource.hikari.connection-timeout
连接超时时间(毫秒,默认 30000) 5000
spring.datasource.hikari.maximum-pool-size
最大连接数(默认 10) 20
spring.datasource.hikari.minimum-idle
最小空闲连接数(默认与 maximum-pool-size
相同) 5
spring.datasource.hikari.idle-timeout
空闲连接超时时间(毫秒,默认 600000) 300000
spring.datasource.hikari.max-lifetime
连接最大存活时间(毫秒,默认 1800000) 1800000
3. JPA/Hibernate 配置 配置项 说明 示例值 spring.jpa.hibernate.ddl-auto
DDL 自动生成策略(none
/update
/create
/create-drop
) update
spring.jpa.show-sql
是否打印 SQL 日志(默认 false
) true
spring.jpa.properties.hibernate.dialect
Hibernate 方言(指定数据库类型,如 MySQL 8) org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql
是否格式化 SQL 输出(默认 false
) true
4. MyBatis 配置 配置项 说明 示例值 mybatis.mapper-locations
MyBatis Mapper XML 文件路径 classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case
是否开启驼峰命名转换(默认 false
) true
mybatis.configuration.log-impl
日志实现(如 STDOUT_LOGGING
) org.apache.ibatis.logging.stdout.StdOutImpl
三、Web 与 HTTP 配置 控制 Web 服务器、HTTP 请求/响应行为、静态资源等。
配置项 说明 示例值 spring.mvc.servlet.load-on-startup
DispatcherServlet 启动时加载顺序(默认 -1,延迟加载) 1
spring.mvc.static-path-pattern
静态资源访问路径(默认 /static/**
、/public/**
等) /assets/**
spring.resources.static-locations
静态资源存储路径(默认 classpath:/META-INF/resources/
等) classpath:/static/
spring.mvc.date-format
日期类型参数的格式化模式(如 yyyy-MM-dd
) yyyy-MM-dd HH:mm:ss
spring.mvc.locale
默认语言环境(影响国际化) zh_CN
spring.mvc.ignore-default-model-on-redirect
重定向时是否忽略默认 Model(默认 true
) false
server.tomcat.max-threads
Tomcat 最大工作线程数(默认 200) 500
server.jetty.max-threads
Jetty 最大工作线程数(默认 200) 500
四、安全配置(Spring Security) 控制认证、授权、CSRF、CORS 等安全行为。
配置项 说明 示例值 spring.security.enabled
是否启用 Spring Security(默认 true
) false
(测试时禁用)spring.security.oauth2.client.registration.github.client-id
GitHub OAuth2 客户端 ID your-github-client-id
spring.security.oauth2.client.registration.github.client-secret
GitHub OAuth2 客户端密钥 your-github-client-secret
spring.security.user.name
内存认证的用户名(简单场景) admin
spring.security.user.password
内存认证的密码 admin123
spring.security.csrf.enabled
是否启用 CSRF 保护(默认 true
) false
(API 场景可能需要关闭)spring.security.cors.allowed-origins
CORS 允许的源(默认 *
) http://localhost:8080
spring.security.cors.allowed-methods
CORS 允许的 HTTP 方法(默认 [GET, POST, PUT, DELETE]
) [GET, POST]
五、缓存配置 控制 Spring Cache 抽象的底层实现(如 Redis、Caffeine)。
1. 通用缓存配置 配置项 说明 示例值 spring.cache.type
缓存类型(simple
/caffeine
/redis
/ehcache
等) redis
spring.cache.cache-names
预定义的缓存名称(逗号分隔) userCache,productCache
2. Caffeine(本地缓存)专用配置 配置项 说明 示例值 spring.cache.caffeine.spec
Caffeine 缓存策略(如 maximumSize=100,expireAfterWrite=10m
) maximumSize=500,expireAfterAccess=5m
spring.cache.caffeine.initial-capacity
初始缓存容量 10
3. Redis(分布式缓存)专用配置 配置项 说明 示例值 spring.redis.host
Redis 服务器地址 localhost
spring.redis.port
Redis 端口(默认 6379) 6380
spring.redis.password
Redis 密码(若有) redis-pass
spring.redis.database
Redis 数据库索引(默认 0) 1
spring.redis.timeout
连接超时时间(毫秒,默认 5000) 3000
spring.redis.lettuce.pool.max-active
Lettuce 连接池最大活跃连接数 8
六、日志配置 控制日志级别、输出格式、文件存储等(支持 Logback、Log4j2 等)。
配置项 说明 示例值 logging.level.root
根日志级别(trace
/debug
/info
/warn
/error
) info
logging.level.com.example
指定包下的日志级别(如 com.example.service
) debug
logging.file.name
日志文件名称(如 app.log
) myapp.log
logging.file.path
日志文件存储路径(默认当前目录) /var/log/myapp/
logging.pattern.console
控制台日志输出格式 %d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
logging.pattern.file
文件日志输出格式 %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
七、消息队列与中间件 集成 RabbitMQ、Kafka、RocketMQ 等消息中间件的配置。
1. RabbitMQ 配置 配置项 说明 示例值 spring.rabbitmq.host
RabbitMQ 服务器地址 localhost
spring.rabbitmq.port
RabbitMQ 端口(默认 5672) 5673
spring.rabbitmq.username
用户名 guest
spring.rabbitmq.password
密码 guest
spring.rabbitmq.virtual-host
虚拟主机 /
spring.rabbitmq.listener.simple.concurrency
消费者最小并发数 3
spring.rabbitmq.listener.simple.max-concurrency
消费者最大并发数 10
2. Kafka 配置 配置项 说明 示例值 spring.kafka.bootstrap-servers
Kafka 服务地址(逗号分隔) localhost:9092
spring.kafka.consumer.group-id
消费者组 ID my-group
spring.kafka.consumer.auto-offset-reset
消费偏移量重置策略(earliest
/latest
) earliest
spring.kafka.producer.retries
生产者重试次数 3
八、邮件配置(Spring Mail) 配置 SMTP 服务器发送邮件。
配置项 说明 示例值 spring.mail.host
SMTP 服务器地址(如 smtp.gmail.com
) smtp.qq.com
spring.mail.port
SMTP 端口(默认 25,SSL 通常为 465) 465
spring.mail.username
发件人邮箱用户名 user@qq.com
spring.mail.password
发件人邮箱密码(或授权码) xxxxxx
spring.mail.properties.mail.smtp.ssl.enable
是否启用 SSL(默认 false
) true
spring.mail.properties.mail.smtp.starttls.enable
是否启用 STARTTLS(默认 false
) true
九、分布式与云原生配置 支持服务发现(Eureka、Consul)、配置中心(Spring Cloud Config)、分布式事务等。
1. Eureka 服务发现 配置项 说明 示例值 eureka.client.service-url.defaultZone
Eureka 服务器地址(多个用逗号分隔) http://eureka-server:8761/eureka/
eureka.client.register-with-eureka
是否注册到 Eureka(默认 true
) true
eureka.client.fetch-registry
是否从 Eureka 获取服务列表(默认 true
) true
eureka.instance.instance-id
实例唯一 ID ${spring.application.name}:${server.port}
2. Consul 服务发现 配置项 说明 示例值 spring.cloud.consul.host
Consul 服务器地址 localhost
spring.cloud.consul.port
Consul 端口(默认 8500) 8501
spring.cloud.consul.discovery.service-name
服务注册名称 my-service
十、Actuator 监控配置 暴露应用健康、指标、端点等信息(用于监控和运维)。
配置项 说明 示例值 management.endpoints.web.exposure.include
暴露的 HTTP 端点(如 health
、info
、metrics
) *
(暴露所有)management.endpoint.health.show-details
是否显示健康检查详情(默认 never
) always
management.metrics.export.prometheus.enabled
是否启用 Prometheus 指标导出 true
management.endpoint.shutdown.enabled
是否启用 shutdown 端点(默认 false
,生产环境禁用) false
十一、其他常用配置 配置项 说明 示例值 spring.servlet.multipart.max-file-size
文件上传最大大小(默认 1MB) 10MB
spring.servlet.multipart.max-request-size
单次请求最大文件总大小 50MB
spring.task.scheduling.pool.size
异步任务线程池大小(默认 1) 10
spring.task.execution.pool.core-size
异步任务核心线程数 5
spring.messages.basename
国际化消息文件基础名称(如 messages
对应 messages.properties
) i18n/messages
spring.jpa.open-in-view
是否允许在视图层保持 JPA 实体打开(默认 true
,可能导致性能问题) false
总结 application.properties
的配置项非常丰富,覆盖了应用运行的几乎所有方面。实际开发中,通常只需配置核心参数 (如端口、数据源)和业务相关参数 (如缓存策略、第三方服务密钥),其他配置可使用 Spring Boot 默认值。对于复杂场景(如分布式事务、自定义 Bean 配置),还可结合 @Configuration
类或 application.yml
的层级结构实现更灵活的配置管理。