JAVA系统中Spring Boot 应用程序的配置文件:application.yml

backendsrcmain esourcesapplication.yml 是一个配置文件,用于定义 Spring Boot 应用程序的各种配置属性。这个文件通常包含数据库连接、服务器设置、日志配置、安全设置以及其他应用程序级别的配置。

文件路径
复制代码
backendsrcmain
esourcesapplication.yml
文件内容

以下是一个典型的 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: 指定应用程序监听的端口号。
  • context-path: 指定应用程序的上下文路径。
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: 指定应用程序的名称。
  • datasource: 数据库连接配置,包括 URL、用户名、密码和驱动类名。
  • jpa :
    • hibernate.ddl-auto : 指定 Hibernate 如何自动处理数据库模式(例如 update, create, create-drop)。
    • show-sql: 是否在控制台显示 SQL 语句。
    • properties.hibernate.dialect: 指定使用的 Hibernate 方言。
    • properties.hibernate.format_sql: 是否格式化 SQL 语句。
  • security.user: 默认用户的安全配置,包括用户名和密码。
3. Logging 配置
复制代码
logging:
  level:
    root: INFO
    com.mechanical.erp: DEBUG
  • root : 设置根日志级别为 INFO。
  • com.mechanical.erp : 设置特定包的日志级别为 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 发行者的 URI。
6. 自定义配置
复制代码
custom:
  app:
    feature-flag:
      new-ui: true
    timeout:
      default: 30s
  • custom.app.feature-flag.new-ui: 自定义功能标志,启用新 UI。
  • custom.app.timeout.default: 自定义默认超时时间。
使用示例

以下是一些常见的配置项及其用途:

数据库连接配置
复制代码
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: 数据库连接 URL。
  • username: 数据库用户名。
  • password: 数据库密码。
  • driver-class-name: JDBC 驱动类名。
JPA 配置
复制代码
spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        format_sql: true
  • ddl-auto: 控制 Hibernate 如何处理数据库模式。
  • show-sql: 是否在控制台显示 SQL 语句。
  • dialect: 指定使用的 Hibernate 方言。
  • format_sql: 是否格式化 SQL 语句。
日志配置
复制代码
logging:
  level:
    root: INFO
    com.mechanical.erp: DEBUG
  • level.root: 设置根日志级别。
  • level.com.mechanical.erp: 设置特定包的日志级别。
管理端点配置
复制代码
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • include: 暴露所有管理端点。
总结
  • application.yml (配置文件) :
    • 目的: 定义 Spring Boot 应用程序的各种配置属性。
    • 内容: 包含服务器配置、Spring 配置、日志配置、安全配置和其他应用程序级别的配置。
    • 作用: 用于配置应用程序的行为和环境,确保应用程序能够正确启动和运行。

确保这个文件中的配置正确无误,并且符合项目的整体需求。

相关推荐
EatFan4 小时前
Spring Boot 4.0 升级清单:JDK 基线、Jakarta 命名空间、Observation API 与第三方组件兼容一次讲清
java·spring boot·后端
辰海Coding4 小时前
平台如何接入一台物联网设备?又该如何设计它的通信协议?
java·物联网
代码民工正电荷4 小时前
JAVA重要知识点和集合
java·开发语言
星恒随风4 小时前
C++11 加餐(4)并发支持库——condition_variable 条件变量与生产者消费者模型
开发语言·c++
利威尔xu4 小时前
Go map 并发十连问:多 goroutine 同时读写触发 fatal error,进程直接崩溃
开发语言·golang
你不知道我是谁?5 小时前
GESP C++ 补充知识:浮点数格式化输出(%f/%e/%g)与 fmod 浮点余数实战
开发语言·c++
九蝶科技5 小时前
运费分摊的二开实现:BigDecimal 精确分摊 + 尾差补齐 + 暂估调整
开发语言·python
李少兄6 小时前
IDEA Apifox Helper 插件踩坑:2.0.15-243 版本“导出指定接口”参数描述丢失问题解析与修复方案
java·ide·intellij-idea
Ryan-Zhong6 小时前
一次接入 16 个平台:视频链接元数据解析 API 的 Python 实战与踩坑记录
开发语言·python·dreamweaver
努力攀登的小k6 小时前
《新手练表:从建表到删表全操作|从零学会表操作》
java·数据库·mysql