🌷🍁 博主猫头虎 带您 Go to New World.✨🍁
🦄 博客首页——猫头虎的博客🎐
🐳《面试题大全专栏》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺
🌊 《IDEA开发秘籍专栏》学会IDEA常用操作,工作效率翻倍~💐
🌊 《100天精通Golang(基础入门篇)》学会Golang语言,畅玩云原生,走遍大小厂~💐
🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬请批评指正!🍁🐥
文章目录
Spring Boot的.yml配置文件通用模板
摘要:
本篇博客将提供一个通用的.yml配置文件模板,适用于Spring Boot项目中的各种配置需求。我们将分享一个基本的模板,帮助您快速开始编写配置文件,以满足您的项目需求。
引言:
Spring Boot使用.yml配置文件来管理应用程序的配置。为了方便开发,一个通用的配置文件模板可以帮助您快速启动新的项目,同时也可以保持一致的配置风格。本文将为您提供一个基本的.yml配置文件模板,适用于大多数Spring Boot项目。
配置方法:
方法一
以下是一个通用的.yml配置文件模板,您可以根据需要进行修改和扩展:
server:
port: 8080
spring:
application:
name: your-application-name
datasource:
url: jdbc:mysql://localhost:3306/your-database
username: your-username
password: your-password
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
profiles:
active: dev
logging:
level:
root: INFO
org.springframework: INFO
# Add more configuration properties here
- 将
server
节点配置为您希望的端口。 - 在
spring
节点下,配置应用程序的名称、数据库连接等。 - 可以根据需求添加更多的配置项,例如Redis、消息队列等。
方法二
SpringBoot 的配置文件通用模板
application.yml 注意事项
在application.yml 文件书写注意:
1. 不同“等级” 用冒号隔开
2. 次等级的前面是空格,不能使用制表符(tab)
3. 冒号之后如果有值,那么冒号和值之间至少有一个空格,不能紧贴着
#服务器设置-----------------------
server:
port: 9090
servlet:
#热部署
jsp:
init-parameters:
development: true
# post表单提交乱码
context-path: /user
tomcat:
uri-encoding: UTF-8
#Spring设置-----------------------
spring:
#编码规则
http:
encoding:
charset: UTF-8
force: true
#全局收参日期格式
mvc:
date-format: yyyy-MM-dd
#视图解析器
view:
prefix: /
suffix: .jsp
#配置数据源(读写分离数据源)
datasource:
#配置自定义数据源1
master:
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 0
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://10.10.0.151:3306/project?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&useSSL=false
#配置自定义数据源2
slave1:
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 0
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://10.10.0.152:3306/project?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&useSSL=false
#配置自定义数据源3
slave2:
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 0
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://10.10.0.152:3306/project?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&useSSL=false
#Mycat数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.10.0.151:8066/mycat #连接mycat逻辑库
username: root
password: root
#单机数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&useSSL=false
# username: root
# password: 123
# Mycat 数据源
#出参的全局格式
jackson:
date-format: yyyy-MM-dd
time-zone: GMT+8
#文件上传配置
servlet:
multipart:
enabled: true
max-file-size: 5MB
max-request-size: 50MB
location: /E:/temp #指定临时目录
#连接redis
redis:
host: 10.10.0.152
port: 6379
timeout: 5s #超时时间
lettuce:
pool:
max-active: 10 #最大活动数
max-idle: 8 #最大闲置数
max-wait: 5ms #最大等待数
min-idle: 1 #最小闲置数
shutdown-timeout: 100ms #超时停机时间
#mybatis设置-----------------------
mybatis:
mapper-locations: classpath:com/libin/mapper/*.xml
type-aliases-package: com.libin.entities
executor-type: batch #开启mybatis的批处理
#设置日志等级-----------------------
logging:
level:
root: error
com.libin.dao: debug
com.libin.service: warn
com.libin.controller: warn
#文件系统
fdfs:
tracker-list: pro1:22122,pro2:22122
# 配置默认缩略图
thumb-image:
height: 80
width: 80
总结:
使用一个通用的.yml配置文件模板可以简化Spring Boot项目的配置过程,并确保配置的一致性。根据项目需求,您可以在模板的基础上进行定制化的配置,以满足不同的业务需求。
参考资料:
- “Common Application properties” in Spring Boot documentation: 链接
- “Spring Boot in Action” by Craig Walls, Manning Publications, 2020.
- “Mastering Spring Boot 2.5” by K. Siva Prasad Reddy, Packt Publishing, 2021.
原创声明
======= ·
- 原创作者: 猫头虎
作者wx: [ libin9iOak ]
学习 | 复习 |
---|---|
✔ |
本文为原创文章,版权归作者所有。未经许可,禁止转载、复制或引用。
作者保证信息真实可靠,但不对准确性和完整性承担责任。
未经许可,禁止商业用途。
如有疑问或建议,请联系作者。
感谢您的支持与尊重。
点击
下方名片
,加入IT技术核心学习团队。一起探索科技的未来,共同成长。