[Java2023] 使用yml替换掉properties配置文件

61 阅读1分钟

1.yml配置文件

注释掉application.properties, 在同级目录下添加application.yml

spring:
  # 数据库链接信息
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/tlias
    username: root
    password: root
  #文件上传
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 100MB

mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true


# 阿里云配置,省略

2.用注解自动将配置注入到类属性中

  • 1.注解自动注入属性

image.png

  • 2.普通方式读取配置: @Value("${xxx.xxx}")

image.png