Spring Boot application.properties和application.yml文件的配置

在Spring Boot中,`application.properties` 和 `application.yml` 文件用于配置应用程序的各个方面,如服务器端口、数据库连接、日志级别等。这两个文件是Spring Boot的配置文件,位于 `src/main/resources` 目录下。

application.properties 示例

`application.properties` 文件使用键值对的格式进行配置:

复制代码
# 设置服务器端口
server.port=8080
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 日志配置
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
# Thymeleaf 配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false

application.yml 示例

`application.yml` 文件使用 YAML(Yet Another Markup Language)格式,它是一种直观的能够被电脑读取的数据序列化格式,并且易于人类阅读。它是JSON的一个超集。

复制代码
# 设置服务器端口
server:
  port: 8080
# 数据库配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: myuser
    password: mypassword
    driver-class-name: com.mysql.cj.jdbc.Driver
# 日志配置
logging:
  level:
    root: INFO
    org.springframework.web: DEBUG
    org.hibernate: ERROR
# Thymeleaf 配置
thymeleaf:
  prefix: classpath:/templates/
  suffix: .html
  mode: HTML
  encoding: UTF-8
  cache: false

在 `application.yml` 文件中,可以使用缩进来表示层级关系,使得配置更加清晰。

注意事项

  • `application.properties` 和 `application.yml` 可以同时存在,但是 `application.properties` 中的配置会覆盖 `application.yml` 中的同名配置。

  • `application.yml` 支持数组或列表的配置,例如:`myprops: \~, \~`。

  • 在 `application.yml` 中,冒号 `:` 后面必须有一个空格。

  • `application.yml` 支持多文档块,可以在同一个文件中分隔多个配置文档。

选择 `application.properties` 还是 `application.yml` 主要取决于个人喜好和项目需求。YAML格式在处理复杂配置时可能更加直观和易于管理。

相关推荐
SomeOtherTime20 小时前
Rust所有权(AI回答)
开发语言·后端·rust
程序员cxuan21 小时前
离谱,AI 大佬顺手搓了个 YouTube ,而我顺手给他搓了个中文插件
人工智能·后端·程序员
学长毕设21 小时前
基于springboot科创竞赛信息平台-附源码
java·spring boot·后端
深蓝AI1 天前
Gemini Decisions API 来了:一次前向打分取代逐字解码,校准误差 0.0250 才是 agent 分支调用的真门槛
人工智能·后端
聪明的笨猪猪1 天前
Java 工程案例:AI 生成 @Async 异步线程池踩坑
java·经验分享·面试·ai编程
学长毕设1 天前
基于SpringBoot公务员考试交流平台-附源码
java·spring boot·后端
IT_陈寒1 天前
Vue的watch把我坑惨了,原来要这样用才靠谱
前端·人工智能·后端
黃芢輝1 天前
开源实战:基于 Spring Boot 3 + Vue 3 + Redis 的高校选修课管理系统(志愿填报 + 统一抽签)
后端
小雨的AI中台笔记1 天前
502 还是 503?「无可用渠道」其实只有三个原因
后端
LCG元1 天前
我让AI重写了这个老系统
后端