当前位置:网站首页>基础篇——配置文件解析
基础篇——配置文件解析
2022-07-05 20:09:00 【printf('小白');】
配置
复制模块
- 删除之前工程的文件,
只留下pom.xml 和 src 文件- 修改pom文件中的
artifactId与新工程一致- 如果不修改name标签的话,尽量把name标签和description标签都删除
- 保留备份即可
属性配置(application.properties)
- 每一个配置对应的是技术,如果不引用某种技术,配置文件中就不会有代码提示
- 官方配置文档
# 服务器端口配置
server.port=80
# 修改banner
#spring.main.banner-mode=console
# 修改banner图
#spring.banner.image.location=ysg.jpg
# 日志
logging.level.root=info
配置文件三种类型
三种共存时properties > yml > yaml- 相同的属性会采用优先级,不同配置则都会保留配置
- properties
yml
yaml
- properties
添加配置文件
- 配置文件自动代码提示

yml配置文件
- 优点
- 容易阅读
容易与脚本交互
以数据核心,重数据轻格式
- 容易阅读
- 语法规则
- 大小写敏感
属性层级关系使用多行描述,每行结尾使用冒号结束
使用缩进表示层级关系,同层级左侧对齐,只允许使用空格
属性值前面添加空格(属性名和属性值之间使用冒号+空格作为分隔) - #表示注释
- 大小写敏感
- yml文件中的各种数据格式
# 服务器端口配置
server:
port: 81
country: china
party: true
birthday: 1949-10-01
null: ~
# 一个对象
user:
name: ssc
age: 23
# 多层级对象
a:
b:
c:
d: 5
# 数组
likes:
- game
- music
- sleep
-
likes1: [game,music,sleep]
# 对象数组
users:
-
name: zhangsan
age: 18
-
name: wangwu
age: 23
users1:
- name: zhangsan
age: 18
- name: wangwu
age: 23
users2: [{
name:zhangsan,age:18},{
name:lisi,age:17}]
读取yml文件的数据
- 注解
@Value配合SpEL读取单个数据,多个数据按层级读取
// 读yml中数据的单一数据
@Value("${country}")
private String country1;
// 对象
@Value("${user.name}")
private String name1;
// 数组
@Value("${likes[1]}")
private String likes1;
// 数组对象
@Value("${users[0].age}")
private String age1;
// 端口号
@Value("${server.port}")
private String port;
变量引用 & 转义字符
# 变量引用
baseDir: c:\windows
tempDir: ${
baseDir}\temp
# 转义字符
tempDir1: "${baseDir}\temp \t1 \t2 \t3 \t4"
// 变量引用
@Value("${tempDir}")
private String tempDir;
// 转义字符
@Value("${tempDir1}")
private String tempDir1;
读取全部yml属性
- 使用Environment 对象封装全部信息
- 使用@Autowired自动装配数据到Environment 对象中
- env.getProperty(“server.port”)读取不同的属性
// 读取全部yml属性,自动装配
@Autowired
private Environment env;
System.out.println(env.getProperty("server.port"));
System.out.println(env);
读取yml引用数据类型(常用)
- yml定义一个对象数据类型
# 需要被读取引用数据类型
datasource:
driver: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: 123456
- 指定对应的类
// spring管理的Bean
@Component
// 指定加载的yml的某一个属性
@ConfigurationProperties(prefix = "datasource")
@Data
public class MyDataSource {
private String driver;
private String url;
private String username;
private String password;
}
- 输出
System.out.println(myDataSource);
边栏推荐
- ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
- 处理文件和目录名
- js方法传Long类型id值时会出现精确损失
- Leetcode brush questions: binary tree 18 (largest binary tree)
- 【c语言】快速排序的三种实现以及优化细节
- .Net分布式事务及落地解决方案
- - Oui. Net Distributed Transaction and Landing Solution
- 炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
- 什么是pyc文件
- 港股将迎“最牛十元店“,名创优品能借IPO突围?
猜你喜欢

Leetcode brush question: binary tree 13 (the same tree)

【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)

95后阿里P7晒出工资单:狠补了这个,真香...

Leetcode brush questions: binary tree 18 (largest binary tree)

Zero cloud new UI design

【数字IC验证快速入门】3、数字IC设计全流程介绍

14. Users, groups, and permissions (14)

B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05

Leetcode skimming: binary tree 12 (all paths of binary tree)

Interviewer: what is the internal implementation of set data types in redis?
随机推荐
ICTCLAS word Lucene 4.9 binding
SecureRandom那些事|真伪随机数
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
Is the education of caiqiantang reliable and safe?
Leetcode skimming: binary tree 12 (all paths of binary tree)
Leetcode brush questions: binary tree 11 (balanced binary tree)
. Net distributed transaction and landing solution
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Build your own website (16)
Debezium series: parsing the default value character set
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
微信小程序正则表达式提取链接
Is it safe for CICC fortune to open an account online?
ACM getting started Day1
中金财富在网上开户安全吗?
Is it safe for Anxin securities to open an account online?
Where is the operation of new bonds? Is it safer and more reliable to open an account
Relationship between floating elements and parent and brother boxes
深度學習 卷積神經網絡(CNN)基礎
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)