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 配置、日志配置、安全配置和其他应用程序级别的配置。
    • 作用: 用于配置应用程序的行为和环境,确保应用程序能够正确启动和运行。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值