Spring Boot 提供數百個自動配置屬性,本文整理官方文檔中的核心屬性分類速查表,涵蓋核心容器、Web MVC、模板引擎、數據源、安全、Actuator 監控等高頻場景,適合開發時快速查閱。
# 配置文件名與位置 spring.config.name=application spring.config.location=classpath:/config/ # Profile 激活 spring.profiles.active=dev spring.profiles.include=common # 應用標識 spring.application.name=my-service spring.application.index=0 # Banner spring.main.show-banner=true
server.port=8080 server.address=0.0.0.0 server.servlet.context-path=/ server.servlet-path=/ server.session-timeout=30m # SSL server.ssl.enabled=true server.ssl.key-store=classpath:keystore.p12 server.ssl.key-store-password=secret server.ssl.key-alias=myapp # Tomcat 專屬 server.tomcat.max-threads=200 server.tomcat.uri-encoding=UTF-8 server.tomcat.access-log-enabled=true server.tomcat.compression=on server.tomcat.compressable-mime-types=text/html,text/xml,application/json
spring.mvc.view.prefix=classpath:/templates/ spring.mvc.view.suffix=.html spring.mvc.date-format=yyyy-MM-dd spring.mvc.locale=zh_CN spring.mvc.favicon.enabled=true # 靜態資源 spring.resources.static-locations=classpath:/static/,classpath:/public/ spring.resources.cache-period=3600
| 引擎 | 關鍵屬性 |
|---|---|
| Thymeleaf | spring.thymeleaf.prefix=classpath:/templates/, suffix=.html, mode=HTML5, cache=true |
| FreeMarker | spring.freemarker.template-loader-path=classpath:/templates/, suffix=.ftl, cache=true |
| Velocity | spring.velocity.resource-loader-path=classpath:/templates/, suffix=.vm, cache=true |
| Groovy Templates | spring.groovy.template.prefix=classpath:/templates/, suffix=.tpl, cache=true |
開發環境建議:cache=false 實現熱刷新。
# 基礎連接 spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false spring.datasource.username=root spring.datasource.password=secret spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 連接池 (HikariCP 默認) spring.datasource.hikari.maximum-pool-size=20 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.idle-timeout=300000 spring.datasource.hikari.connection-timeout=20000 # JPA / Hibernate spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.open-in-view=false # 避免 Open Session In View 反模式 # Flyway 遷移 flyway.enabled=true flyway.locations=classpath:db/migration flyway.baseline-on-migrate=true
# 基礎認證 security.user.name=admin security.user.password=admin123 security.user.role=ADMIN # 進階 security.basic.enabled=true security.basic.realm=Spring security.require-ssl=false security.enable-csrf=false security.headers.frame=false security.sessions=stateless security.ignored=/public/**,/health,/info
spring.cache.type=redis # 或 ehcache, hazelcast, caffeine, simple spring.cache.cache-names=users,products spring.cache.redis.time-to-live=600000 spring.cache.redis.key-prefix=myapp:
| 中間件 | 關鍵屬性 |
|---|---|
| RabbitMQ | spring.rabbitmq.host, port, username, password, virtual-host |
| Redis | spring.redis.host, port, password, database, lettuce.pool.* |
| ActiveMQ | spring.activemq.broker-url, user, password, pooled |
| Kafka | spring.kafka.bootstrap-servers, consumer/producer.* |
management.endpoints.web.exposure.include=health,info,metrics,prometheus management.endpoint.health.show-details=always management.health.db.enabled=true management.health.redis.enabled=true management.info.env.enabled=true management.server.port=8081 # 獨立管理端口 management.server.ssl.enabled=true
# Micrometer + Prometheus
management.metrics.export.prometheus.enabled=true
management.metrics.tags.application=${spring.application.name}
# Zipkin/Sleuth
spring.zipkin.base-url=https://localhost:9411
spring.sleuth.sampler.probability=1.0
--debug 打印自動配置報告,查看生效屬性/actuator/configprops 端點查看所有綁定配置