主要介绍两种配置文件的语法和格式,properties和yml
目录
1.配置文件的作用
对于配置文件,也有独立的文件夹去存放,主要用来存放一些需要经过变动的数据(变量值);后续这些变量的值需要改变的话,直接在配置文件中修改值即可,就不需要一个个变量去修改了。
通常需要存放在配置文件中的数据
(1)项目的启动端口
(2)数据库的连接信息(如:用户名和密码)
(3)第三方系统的调用密钥等信息(如:验证当前客户端是否正确)
(4)用于发现和定位问题的普通日志和异常日志等
概念听起来都很很迷糊,我们直接开始教学
2.创建配置文件
这个还需要教吗?当然需要。
(1)创建文件的位置

当项目创建好之后,一般都会默认存在application.properties这个配置文件,下面的application.yml是我后面创建的。
(2)创建文件的名字
名字是很固定的,必须是:application.properties或者application.yml,这两个都是配置文件的格式,一般我们使用其中一个即可(一般yml使用的较多)。还有一种是application.yaml,这种其实是yml的缩写,所以我们就不会多介绍了。
(3)当项目中properties和yml都存在,并且里面的内容冲突时,会以properties中的为配置项,也就是其优先级更高
3.properties语法
首先,我们先认识一下界面
双击点开:

我们就会来到这个页面,就在这个页面就行编写配置内容

(1)语法格式
通过上面我们可以看到,通过=连接。
- 语法规则:key和value之间通过=连接,key和key之间使用.连接
=左边的就是key,=右边的是value,在获取配置文件时,需要填入完整的key。
在配置文件中,使用#号进行注释
- 举例
配置端口号:有提示可以直接使用


这个有什么用呢?我们启动一下程序即可知道

配置数据库相关信息:
java
spring.datasoure.url = jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8&useSSL=false
spring.datasource.username=zhangsan
spring.datasource.password=123456

其中,key 和value我们都是可以自定义的。
上面介绍了如何配置文件内容,接下来,就是要去代码中获取key对应的值
(2)获取配置文件
java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/con")
@RestController
public class Controller {
//获取端口号
@Value("${server.port}")
private String port;
@RequestMapping("/get")
public String getValue() {
return "port="+port;
}
}
运行结果:

原理:程序运行之后,会将对应的值存放在port中;发挥作用的是@Value注解,然后里面格式为:"${}",花括号内就是路径,也就是key。
也可以通过其他的注解获取(不需要借助浏览器或者postman),我们在方法前面加上@PostConstruct注解,当项目运行起来后,就会默认执行该方法。
代码:
java
//获取url
@Value("${spring.datasoure.url}")
private String url;
@PostConstruct
public void get() {
System.out.println("================");
System.out.println("spring.datasoure.url"+url);
System.out.println("================");
}

运行结果:

以上就是获取配置文件的方法了。
(3).properties的缺点
为什么要介绍缺点,那是为了方便介绍yml,更有对比性。
缺点:存在很多的重复项

每写一个配置项,都需要重新写一长串前缀,就会显得很冗余。所以,我们需要再去学习yml
4.yml语法
(1)语法格式
格式:key: value (冒号后面有一个空格,不可以省略),如果多个key,也就是每个key中间由点好分割,在yml格式下就要改成冒号+换行分割

像上面的就是大致的写法,冒号后面的空格一定不要忘。还有一点就是:其中null使用~表示,其他的类型比如整型和字符串,使用什么类型去接受,就可以自动转换成什么类型
(2)获取配置文件
yml格式获取配置文件和properties是一样的
java
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class YmlController {
@Value("${string.str1}")
private String str1;
@Value("${string.str2}")
private String str2;
@Value("${string.str3}")
private String str3;
@Value("${mykey}")
private String value;
@Value("${key.you.love}")
private String love;
@Value("${test}")
private int num;
@PostConstruct
public void getValue() {
System.out.println(value);
System.out.println("========================");
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println("========================");
}
}
一样一样的,文件的路径也是一样,都使用点号连接。但是yml在编写的时候格式不同怎么获取呢?下面教学。

只需要选中key,然后右键,然后复制到"${}"中即可。
(3)yml中的单双引号的差异

打印结果:

一般双引号里面的东西不会被被转义,单引号里面的会被转义,也就是\n在单引号下就只是一个串,在双引号下是换行
(4)配置对象
上述是配置普通类型,下面介绍配置对象

获取整个对象不能再使用@Value注解,而使用:@ConfigurationProperties


打印结果:

这就是获取对象的方法
(5)获取集合
获取list

获取set:

打印结果:


获取map:

(6)yml优缺点
优点:
1.可读性高,写法简单,易于理解
2.支持更多的数据类型,可以简单表达对象
3.支持更多的编程语言
缺点:不适合写复杂的配置文件
以上就是全部语法内容了。
5.配置文件格式
我们只需要修改成UTF-8编码即可,也就是支持中文注释

(1)配置当前文件
在seting搜索栏中输入:File encodings,然后出现下面的页面


上述只是将当前项目修改
(2)修改后续创建的所有项目

进入后继续搜索:File encodings

以上就是本节的全部内容了。