SpringBoot配置文件中常用配置属性详解(application.properties/application.yml)
目录
- 前言
- 1. 服务器相关配置(Server Properties)
- application.properties 示例
- application.yml 示例
- 常见配置说明
- 2. 数据源配置(DataSource Properties)
- application.properties 示例
- application.yml 示例
- 常见配置说明
- 3. JPA / Hibernate 配置(Spring Data JPA)
- application.properties 示例
- application.yml 示例
- 常见配置说明
- 4. 日志配置(Logging)
- application.properties 示例
- application.yml 示例
- 常见配置说明
- 5. 模板引擎配置(Thymeleaf)
- application.properties 示例
- application.yml 示例
- 6. 安全配置(Spring Security)
- application.properties 示例
- application.yml 示例
- 7. 缓存配置(Cache)
- application.properties 示例
- application.yml 示例
- 8. 任务调度配置(Scheduling)
- application.properties 示例
- application.yml 示例
- 9. 国际化配置(i18n)
- application.properties 示例
- application.yml 示例
- 10. 其他常用配置
- 10.1 文件上传配置
- 10.2 WebMvc 配置
- 10.3 Actuator 配置
- 总结
前言
Spring Boot 的一大优势就是通过简单的配置文件即可快速定制应用行为,而无需编写大量 XML 配置或 Java 代码。Spring Boot 使用 application.properties
或 application.yml
作为核心配置文件,支持丰富的配置属性。
本文将详细介绍 Spring Boot 常用的配置属性,包括:
- 服务器配置
- 数据源配置
- JPA / Hibernate 配置
- 日志配置
- Thymeleaf / 模板引擎配置
- 安全配置(Spring Security)
- 缓存配置
- 任python务调度配置
- 国际化配置
- 其他常用配置
1. 服务器相关配置(Server Properties)
控制嵌入式服务器(如 Tomcat、Jetty)的行为。
application.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 示例
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=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 示例
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=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 示例
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=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 示例
logging: level: root: INFO com.example.demo: DEBUG file: name: logs/app.log max-size: 10MB pattern: console: "%d{yyyy-MM-dd HH:mpythonm:ss} - %msg%n"
常见配置说明
属性名 | 说明 |
---|---|
logging.level.* | 设置不同包的日志级别 |
logging.file.name | 日志输出文件路径 |
logging.file.max-size | 日志文件最大大小 |
logging.pattern.console | 控制台日志输出格式 |
5. 模板引擎配置(Thymeleaf)
如果你使用 Thymeleaf 模板引擎,可以配置缓存、模板路径等。
application.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 示例
spring: thymeleaf: cache: false prefix: classpath:/templates/ suffix: .html mode: HTML encoding: UTF-8
6. 安全配置(Spring Security)
用于配置 Spring Securandroidity 的默认行为。
application.properties 示例
spring.security.user.name=admin spring.security.user.password=123456 spring.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=simple spring.cache.cache-names=myCache spring.cache.simple.initial-capacity=100 spring.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=messages spring.messages.encoding=UTF-8 spring.locale=zh
application.yml 示例
spring: messages: basename: messages encoding: UTF-8 locale: zh
10. 其他常用配置
10.1 文件上传配置
spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB
spring: servlet: multipart: max-file-size: 10MB max-requandroidest-size: 10MB
10.2 WebMvc 配置
spring.mvc.async.request-timeout=0 spring.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.properties
或 application.yml
可以快速配置服务器、数据库、日志、缓存、安全等多个模块的行为。使用合适的配置,可以显著提升开发效率和系统稳定性。
建议:在开发阶段启用更多调试信息(如 SQL 打印),在生产环境中关闭调试输出并启用缓存、日志分割等优化配置。
到此这篇关于SpringBoot配置文件中常用配置属性详解(application.properties/application.yml)的文章就介绍到这了,更多相关SpringBoot配置文件配置属性内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(pythonwww.devze.com)!
精彩评论