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

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

文件路径

复制代码
backend\src\main\resources\application.yml

文件内容

以下是一个典型的 application.yml 文件的示例:

yaml 复制代码
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 配置
yaml 复制代码
server:
  port: 8080
  servlet:
    context-path: /erp
  • port: 指定应用程序监听的端口号。
  • context-path: 指定应用程序的上下文路径。
2. Spring 配置
yaml 复制代码
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 配置
yaml 复制代码
logging:
  level:
    root: INFO
    com.mechanical.erp: DEBUG
  • root : 设置根日志级别为 INFO
  • com.mechanical.erp : 设置特定包的日志级别为 DEBUG
4. Management 配置
yaml 复制代码
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • endpoints.web.exposure.include: 暴露所有管理端点。
5. Security 配置
yaml 复制代码
security:
  oauth2:
    resourceserver:
      jwt:
        issuer-uri: https://your-auth-server.com/oauth/token
  • oauth2.resourceserver.jwt.issuer-uri: 指定 JWT 发行者的 URI。
6. 自定义配置
yaml 复制代码
custom:
  app:
    feature-flag:
      new-ui: true
    timeout:
      default: 30s
  • custom.app.feature-flag.new-ui: 自定义功能标志,启用新 UI。
  • custom.app.timeout.default: 自定义默认超时时间。

使用示例

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

数据库连接配置
yaml 复制代码
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 配置
yaml 复制代码
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 语句。
日志配置
yaml 复制代码
logging:
  level:
    root: INFO
    com.mechanical.erp: DEBUG
  • level.root: 设置根日志级别。
  • level.com.mechanical.erp: 设置特定包的日志级别。
管理端点配置
yaml 复制代码
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • include: 暴露所有管理端点。

总结

  • application.yml (配置文件) :
    • 目的: 定义 Spring Boot 应用程序的各种配置属性。
    • 内容: 包含服务器配置、Spring 配置、日志配置、安全配置和其他应用程序级别的配置。
    • 作用: 用于配置应用程序的行为和环境,确保应用程序能够正确启动和运行。

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

相关推荐
CodeWithMe1 分钟前
【C/C++】namespace + macro混用场景
c语言·开发语言·c++
蓝婷儿9 分钟前
6个月Python学习计划 Day 17 - 继承、多态与魔术方法
开发语言·python·学习
Mikhail_G33 分钟前
Python应用变量与数据类型
大数据·运维·开发语言·python·数据分析
BillKu35 分钟前
Java + Spring Boot + Mybatis 插入数据后,获取自增 id 的方法
java·tomcat·mybatis
全栈凯哥36 分钟前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
chxii37 分钟前
12.7Swing控件6 JList
java
全栈凯哥39 分钟前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表
YuTaoShao39 分钟前
Java八股文——集合「List篇」
java·开发语言·list
PypYCCcccCc44 分钟前
支付系统架构图
java·网络·金融·系统架构
华科云商xiao徐1 小时前
Java HttpClient实现简单网络爬虫
java·爬虫