首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring Boot 常用應用屬性完整參考:核心、Web、數據源、安全、監控等

     編輯:Java 綜合教學

Spring Boot 常用應用屬性完整參考:核心、Web、數據源、安全、監控等

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 與視圖解析

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

模板引擎配置

引擎關鍵屬性
Thymeleafspring.thymeleaf.prefix=classpath:/templates/, suffix=.html, mode=HTML5, cache=true
FreeMarkerspring.freemarker.template-loader-path=classpath:/templates/, suffix=.ftl, cache=true
Velocityspring.velocity.resource-loader-path=classpath:/templates/, suffix=.vm, cache=true
Groovy Templatesspring.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:

消息隊列

中間件關鍵屬性
RabbitMQspring.rabbitmq.host, port, username, password, virtual-host
Redisspring.redis.host, port, password, database, lettuce.pool.*
ActiveMQspring.activemq.broker-url, user, password, pooled
Kafkaspring.kafka.bootstrap-servers, consumer/producer.*

Actuator 監控端點

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

常用屬性查找技巧

  1. IDEA 插件 Spring Boot Properties 支持屬性提示與文檔跳轉
  2. 啟動時加參數 --debug 打印自動配置報告,查看生效屬性
  3. 訪問 /actuator/configprops 端點查看所有綁定配置
  4. 官方文檔附錄:Application Properties
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved