前言
Spring Boot 的一大優勢就是通過簡單的配置文件即可快速定制應用行為,而無需編寫大量 XML 配置或 Java 代碼。Spring Boot 使用 application.properties
或 application.yml
作為核心配置文件,支持豐富的配置屬性。
本文將詳細介紹 Spring Boot 常用的配置屬性,包括:
- 服務器配置
- 數據源配置
- JPA / Hibernate 配置
- 日志配置
- Thymeleaf / 模板引擎配置
- 安全配置(Spring Security)
- 緩存配置
- 任務調度配置
- 國際化配置
- 其他常用配置
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: 8080servlet:context-path: /apitomcat:max-connections: 10000max-http-form-post-size: 20MBerror: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=UTCusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driverhikari:maximum-pool-size: 10idle-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: updateshow-sql: trueproperties:hibernate:format_sql: truedialect: org.hibernate.dialect.MySQL8Dialectopen-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: INFOcom.example.demo: DEBUGfile:name: logs/app.logmax-size: 10MBpattern: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=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
application.yml 示例:
spring:thymeleaf:cache: falseprefix: classpath:/templates/suffix: .htmlmode: HTMLencoding: UTF-8
6. 安全配置(Spring Security)
用于配置 Spring Security 的默認行為。
application.properties 示例:
spring.security.user.name=admin
spring.security.user.password=123456
spring.security.user.roles=USER,ADMIN
application.yml 示例:
spring:security:user:name: adminpassword: 123456roles:- 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: simplecache-names: myCachesimple:initial-capacity: 100max-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: messagesencoding: UTF-8locale: zh
10. 其他常用配置
10.1 文件上傳配置
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring:servlet:multipart:max-file-size: 10MBmax-request-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: 0format: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 打印),在生產環境中關閉調試輸出并啟用緩存、日志分割等優化配置。