目录
2.3 yml 和 properties 连接数据库的配置对比
1.配置文件的必要性
整个项目所有重要的数据都是在配置文件中配置的,如:
- 数据库的连接信息(包含用户名和密码的设置);
- 项目的启动端口;
- 第三方系统的调⽤秘钥等信息;
- 用于发现和定位问题的普通日志和异常日志等。
如果没有配置文件,那么 Spring Boot 项目就不能连接和操作数据库,甚至是不能保存可以用于排查问题的关键日志。
2.配置文件的格式
- .properties
- .yml
注意事项:
1.两个文件从功能上来讲是可以同时存在的,都是企业一般会采取统一的配置文件格式
2.当 properties 和 yml ⼀起存在⼀个项目中时,如果配置文件中出现了同样的配置,比如 properties 和 yml 中都配置了“server.port”, 那么这个时候会以 properties 中的配置为主,也就是 .properties 配置文件的优先级最高,但加载完 .properties 文件之后,也会加载 .yml 文件的配置信息。
2.1 .properties配置文件
properties 配置文件是最早期的配置文件格式,也是创建 Spring Boot 项目默认的配置文件。
2.1.1 properties 基本语法
使配置文件支持中文
当前项目配置文件的设置
新项目配置文件的设置
properties 是以键值的形式配置的:
2.1.2 properties读取配置文件
@Component
public class PostConstruct {
@Value("${server.port}")
private String port;
@javax.annotation.PostConstruct
//当一个class被注解为一个Bean,那么class上被@PostConstruct注解的方法将会在程序启动的时候执行。
public void postConstruct(){
System.out.println(port);
}
}
执行结果:
2.1.3 properties优缺点
优点:
- 系统默认的配置文件
- 优先级比yml高
- 格式简单,不容易出错
缺点:写法比较冗余
2.2 .yml 配置文件
yml 是 YAML 是缩写,它的全称 Yet Another Markup Language 翻译成中文就是“另一种标记语言”
2.2.1 yml 基本语法
key 和 value 之间使用英文冒汗加空格的方式组成的,其中的空格不可省略。
2.2.2 yml不同数据类型
2.2.3 yml 配置读取
@Component
public class PostConstruct {
@Value("${string.hello}") //此处的名称要和.yml中的一致
private String hello;
@javax.annotation.PostConstruct
public void postConstruct(){
System.out.println("hello: "+hello);
}
}
执行结果:
2.2.4 value 值加单双引号的区别
application.yml的代码如下:
string.hello1: 你\n好
string.hello2: '你\n好'
string.hello3: "你\n好"
@Component类的代码如下:
@Component
public class PostConstruct {
@Value("${string.hello1}") //此处的名称要和.yml中的一致
private String hello1;
@javax.annotation.PostConstruct
public void postConstruct1(){
System.out.println("hello1: "+hello1);
}
@Value("${string.hello2}") //此处的名称要和.yml中的一致
private String hello2;
@javax.annotation.PostConstruct
public void postConstruct2(){
System.out.println("hello2: "+hello2);
}
@Value("${string.hello3}") //此处的名称要和.yml中的一致
private String hello3;
@javax.annotation.PostConstruct
public void postConstruct3(){
System.out.println("hello3: "+hello3);
}
}
执行结果:
注意事项:
字符串默认不用加上单引号或者双引号。
单引号会转义特殊字符,特殊字符最终只是⼀个普通的字符串数据。
双引号不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思。
2.2.5 yml配置对象
application.yml的代码如下:
student: {id: 1,name: Java,age: 18}
@Component类的代码如下:
@ConfigurationProperties(prefix = "student")
@Component
@Setter
@Getter
@ToString
public class PostConstruct {
// 这个时候就不能⽤ @Value 来读取配置中的对象了,此时要使⽤另⼀个注解
// @ConfigurationProperties 来读取
private int id;
private String name;
private int age;
}
调用类代码如下:
@Component
public class APP {
@Autowired
private PostConstruct postConstruct;
@javax.annotation.PostConstruct
public void postConstruct() {
System.out.println(postConstruct);
}
}
执行结果:
2.2.6 yml的优点
- yml 通用性更好,支持更多语言,如 Java、Go、Python 等,如果是云服务器开发,可以使用⼀份 配置文件作为 Java 和 Go 的共同配置文件。
- yml 支持更多的数据类型。
2.3 yml 和 properties 连接数据库的配置对比
yml:
spring:
datasource:
url: jdbc://mysql://127.0.0.0:3306/testdb?characterEncoding=utf8
username: root
password: 111111
properties:
spring.datasource.url=jdbc://mysql://127.0.0.0:3306/testdb?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=11111
3.总结
properties 和 yml 的区别:
- properties 是以 key=value 的形式配置的键值类型的配置文件,而 yml 使用的是类似 json 格式的 树形配置方式进行配置的,yml 层级之间使用换行缩进的方式配置,key 和 value 之间使用“: ”英文冒号加空格的方式设置,并且空格不可省略。
- properties 为早期并且默认的配置文件格式,但其配置存在⼀定的冗余数据,使用 yml 可以很好的 解决数据冗余的问题。
- yml 通用性更好,⽀持更多语言,如 Java、Go、Python 等,如果是云服务器开发,可以使用⼀份 配置文件作为 Java 和 Go 的共同配置⽂件。
- yml 支持更多的数据类型。