欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot配置文件中常用配置屬性詳解(application.properties/application.yml)

 更新時間:2025年07月28日 08:53:28   作者:超級小忍  
Spring Boot 的一大優(yōu)勢就是通過簡單的配置文件即可快速定制應(yīng)用行為,而無需編寫大量 XML 配置或 Java 代碼,下面我們就來看看application.properties和application.yml作為核心配置文件的常見屬性吧

前言

Spring Boot 的一大優(yōu)勢就是通過簡單的配置文件即可快速定制應(yīng)用行為,而無需編寫大量 XML 配置或 Java 代碼。Spring Boot 使用 application.propertiesapplication.yml 作為核心配置文件,支持豐富的配置屬性。

本文將詳細(xì)介紹 Spring Boot 常用的配置屬性,包括:

  • 服務(wù)器配置
  • 數(shù)據(jù)源配置
  • JPA / Hibernate 配置
  • 日志配置
  • Thymeleaf / 模板引擎配置
  • 安全配置(Spring Security)
  • 緩存配置
  • 任務(wù)調(diào)度配置
  • 國際化配置
  • 其他常用配置

1. 服務(wù)器相關(guān)配置(Server Properties)

控制嵌入式服務(wù)器(如 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應(yīng)用監(jiān)聽的端口,默認(rèn) 8080
server.servlet.context-path應(yīng)用的上下文路徑,默認(rèn)為空
server.tomcat.max-connectionsTomcat 最大連接數(shù)
server.tomcat.max-http-form-post-sizeHTTP 表單 POST 最大大小
server.error.whitelabel.enabled是否啟用默認(rèn)錯誤頁面(關(guān)閉后返回 JSON 錯誤信息)

2. 數(shù)據(jù)源配置(DataSource Properties)

用于配置數(shù)據(jù)庫連接池,常見如 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數(shù)據(jù)庫連接 URL
spring.datasource.username數(shù)據(jù)庫用戶名
spring.datasource.password數(shù)據(jù)庫密碼
spring.datasource.driver-class-name數(shù)據(jù)庫驅(qū)動類名
spring.datasource.hikari.*HikariCP 特定配置(如最大連接數(shù)、空閑超時)

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.dialectHibernate 方言
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:mm:ss} - %msg%n"

常見配置說明

屬性名說明
logging.level.*設(shè)置不同包的日志級別
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 Security 的默認(rèn)行為。

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

注意:實際項目中建議使用數(shù)據(jù)庫認(rèn)證,而不是配置文件方式。

7. 緩存配置(Cache)

Spring Boot 支持多種緩存實現(xiàn),如 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. 任務(wù)調(diào)度配置(Scheduling)

啟用定時任務(wù)并配置線程池。

application.properties 示例

spring.task.scheduling.pool.size=5

application.yml 示例

spring:
  task:
    scheduling:
      pool:
        size: 5

在代碼中使用 @Scheduled 注解即可定義定時任務(wù)。

9. 國際化配置(i18n)

配置消息源和默認(rèn)語言。

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-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: 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

總結(jié)

Spring Boot 的配置文件非常靈活,通過 application.propertiesapplication.yml 可以快速配置服務(wù)器、數(shù)據(jù)庫、日志、緩存、安全等多個模塊的行為。使用合適的配置,可以顯著提升開發(fā)效率和系統(tǒng)穩(wěn)定性。

建議:在開發(fā)階段啟用更多調(diào)試信息(如 SQL 打印),在生產(chǎn)環(huán)境中關(guān)閉調(diào)試輸出并啟用緩存、日志分割等優(yōu)化配置。

到此這篇關(guān)于SpringBoot配置文件中常用配置屬性詳解(application.properties/application.yml)的文章就介紹到這了,更多相關(guān)SpringBoot配置文件配置屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot?jwt的token如何刷新

    SpringBoot?jwt的token如何刷新

    這篇文章主要給大家介紹了關(guān)于SpringBoot?jwt的token如何刷新的相關(guān)資料,Json web token(JWT)是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開放標(biāo)準(zhǔn),需要的朋友可以參考下
    2023-07-07
  • SpringBoot整合kaptcha實現(xiàn)圖片驗證碼功能

    SpringBoot整合kaptcha實現(xiàn)圖片驗證碼功能

    這篇文章主要介紹了SpringBoot整合kaptcha實現(xiàn)圖片驗證碼功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Java實現(xiàn)在線聊天功能

    Java實現(xiàn)在線聊天功能

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)在線聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • java實現(xiàn)批量導(dǎo)入.csv文件到mysql數(shù)據(jù)庫

    java實現(xiàn)批量導(dǎo)入.csv文件到mysql數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)批量導(dǎo)入.csv文件到mysql數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 在maven工程里運行java main方法

    在maven工程里運行java main方法

    這篇文章主要介紹了在maven工程里運行java main方法,需要的朋友可以參考下
    2014-04-04
  • Spring MVC如何使用@RequestParam注解獲取參數(shù)

    Spring MVC如何使用@RequestParam注解獲取參數(shù)

    這篇文章主要介紹了Spring MVC實現(xiàn)使用@RequestParam注解獲取參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 一鍵打包壓縮,Java項目變身JAR

    一鍵打包壓縮,Java項目變身JAR

    想要一鍵打包Java項目生成JAR文件并進(jìn)行壓縮?本指南將帶你輕松駕馭這項看似復(fù)雜的任務(wù),讓我們一起揭開神秘的面紗,輕松打包,高效出發(fā)!
    2023-12-12
  • 解釋:int型默認(rèn)值為0的問題

    解釋:int型默認(rèn)值為0的問題

    這篇文章主要介紹了解釋:int型默認(rèn)值為0的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java集合框架之List ArrayList LinkedList使用詳解刨析

    Java集合框架之List ArrayList LinkedList使用詳解刨析

    早在 Java 2 中之前,Java 就提供了特設(shè)類。比如:Dictionary, Vector, Stack, 和 Properties 這些類用來存儲和操作對象組。雖然這些類都非常有用,但是它們?nèi)鄙僖粋€核心的,統(tǒng)一的主題。由于這個原因,使用 Vector 類的方式和使用 Properties 類的方式有著很大不同
    2021-10-10
  • 基于SpringBoot?使用?Flink?收發(fā)Kafka消息的示例詳解

    基于SpringBoot?使用?Flink?收發(fā)Kafka消息的示例詳解

    這篇文章主要介紹了基于SpringBoot?使用?Flink?收發(fā)Kafka消息,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01

最新評論