JAVA系統(tǒng)中Spring?Boot應(yīng)用程序的配置文件application.yml使用詳解
backend\src\main\resources\application.yml 是一個配置文件,用于定義 Spring Boot 應(yīng)用程序的各種配置屬性。這個文件通常包含數(shù)據(jù)庫連接、服務(wù)器設(shè)置、日志配置、安全設(shè)置以及其他應(yīng)用程序級別的配置。
文件路徑
backend\src\main\resources\application.yml
文件內(nèi)容
以下是一個典型的 application.yml 文件的示例:
server:
port: 8080
servlet:
context-path: /erp
spring:
application:
name: mechanical-erp-backend
datasource:
url: jdbc:mysql://localhost:3306/mechanical_erp?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
security:
user:
name: admin
password: admin123
logging:
level:
root: INFO
com.mechanical.erp: DEBUG
management:
endpoints:
web:
exposure:
include: "*"
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://your-auth-server.com/oauth/token
# 其他自定義配置
custom:
app:
feature-flag:
new-ui: true
timeout:
default: 30s
解釋
1. Server 配置
server:
port: 8080
servlet:
context-path: /erp
- port: 指定應(yīng)用程序監(jiān)聽的端口號。
- context-path: 指定應(yīng)用程序的上下文路徑。
2. Spring 配置
spring:
application:
name: mechanical-erp-backend
datasource:
url: jdbc:mysql://localhost:3306/mechanical_erp?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
security:
user:
name: admin
password: admin123
- application.name: 指定應(yīng)用程序的名稱。
- datasource: 數(shù)據(jù)庫連接配置,包括 URL、用戶名、密碼和驅(qū)動類名。
- jpa:
- hibernate.ddl-auto: 指定 Hibernate 如何自動處理數(shù)據(jù)庫模式(例如
update,create,create-drop)。 - show-sql: 是否在控制臺顯示 SQL 語句。
- properties.hibernate.dialect: 指定使用的 Hibernate 方言。
- properties.hibernate.format_sql: 是否格式化 SQL 語句。
- hibernate.ddl-auto: 指定 Hibernate 如何自動處理數(shù)據(jù)庫模式(例如
- security.user: 默認(rèn)用戶的安全配置,包括用戶名和密碼。
3. Logging 配置
logging:
level:
root: INFO
com.mechanical.erp: DEBUG
- root: 設(shè)置根日志級別為
INFO。 - com.mechanical.erp: 設(shè)置特定包的日志級別為
DEBUG。
4. Management 配置
management:
endpoints:
web:
exposure:
include: "*"
- endpoints.web.exposure.include: 暴露所有管理端點。
5. Security 配置
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://your-auth-server.com/oauth/token
- oauth2.resourceserver.jwt.issuer-uri: 指定 JWT 發(fā)行者的 URI。
6. 自定義配置
custom:
app:
feature-flag:
new-ui: true
timeout:
default: 30s
- custom.app.feature-flag.new-ui: 自定義功能標(biāo)志,啟用新 UI。
- custom.app.timeout.default: 自定義默認(rèn)超時時間。
使用示例
以下是一些常見的配置項及其用途:
數(shù)據(jù)庫連接配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mechanical_erp?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
- url: 數(shù)據(jù)庫連接 URL。
- username: 數(shù)據(jù)庫用戶名。
- password: 數(shù)據(jù)庫密碼。
- driver-class-name: JDBC 驅(qū)動類名。
JPA 配置
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
- ddl-auto: 控制 Hibernate 如何處理數(shù)據(jù)庫模式。
- show-sql: 是否在控制臺顯示 SQL 語句。
- dialect: 指定使用的 Hibernate 方言。
- format_sql: 是否格式化 SQL 語句。
日志配置
logging:
level:
root: INFO
com.mechanical.erp: DEBUG
- level.root: 設(shè)置根日志級別。
- level.com.mechanical.erp: 設(shè)置特定包的日志級別。
管理端點配置
management:
endpoints:
web:
exposure:
include: "*"
- include: 暴露所有管理端點。
總結(jié)
application.yml(配置文件):- 目的: 定義 Spring Boot 應(yīng)用程序的各種配置屬性。
- 內(nèi)容: 包含服務(wù)器配置、Spring 配置、日志配置、安全配置和其他應(yīng)用程序級別的配置。
- 作用: 用于配置應(yīng)用程序的行為和環(huán)境,確保應(yīng)用程序能夠正確啟動和運行。
確保這個文件中的配置正確無誤,并且符合項目的整體需求。
到此這篇關(guān)于JAVA系統(tǒng)中Spring Boot應(yīng)用程序的配置文件application.yml的文章就介紹到這了,更多相關(guān)Spring Boot配置文件application.yml內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
字節(jié)碼調(diào)教入口JVM?寄生插件javaagent
這篇文章主要介紹了字節(jié)碼調(diào)教入口JVM?寄生插件javaagent方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
springBoot靜態(tài)資源加載不到,并且配置了也不生效問題及解決
這篇文章總結(jié)了一個在Spring Boot 2.6.x版本中,由于路徑匹配策略改變導(dǎo)致靜態(tài)資源無法加載的問題,并提供了解決方案:通過配置類或在配置文件中設(shè)置路徑匹配策略為AntPathMatcher,或者直接降級Spring Boot版本2025-02-02
阿里規(guī)范:為何boolean類型變量命名禁用is開頭
這篇文章主要給大家介紹了關(guān)于阿里規(guī)范:為何boolean類型變量命名禁用is開頭的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java基于HttpClient實現(xiàn)RPC的示例
HttpClient可以實現(xiàn)使用Java代碼完成標(biāo)準(zhǔn)HTTP請求及響應(yīng)。本文主要介紹了Java基于HttpClient實現(xiàn)RPC,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Java實現(xiàn)獲取兩個日期之間的所有日期的開始時間集合
這篇文章主要為大家詳細(xì)介紹了如何使用Java實現(xiàn)獲取兩個日期之間的所有日期的開始時間集合,文中的示例代碼簡潔易懂,有需要的小伙伴可以參考下2026-03-03

