一 普通properties配置
1.代码视图2.结果
【注意】properties配置文件在写中文的时候,会有乱码 , 我们需要去IDEA中设置编码格式为UTF-8;
settings–>FileEncodings 中配置;
二 yaml 配置
我们再添加一个person类
1.代码爆红的,在pom文件加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
@ConfigurationProperties(prefix = "person")
@Component//注册bean到容器中
public class Person {
private String name;
private int age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//加入有参 无参 get set tostring
}
yaml配置文件,注意空格缩进
person:
name: 潇潇
age: 25
happy: false
birth: 2020/3/12
maps: {k1: v1,k2: v2,k3: v3}
lists: [item1,item2,item3]
dog:
name: 小七
age: 2
将yaml配置文件的key 值 和 属性的值设置为不一样,则结果输出为null,注入失败
2.执行yaml配置注入到实体类完全OK!
三加载指定的properties配置文件
@PropertySource :加载指定的配置文件;
@configurationProperties:默认从全局配置文件中获取值;
1.我们去在resources目录下新建一个person.properties文件,并执行结果2.如果此时图上的提示 application.properties中有影响的值,我们会发现如下图
来源:CSDN
作者:记录编码路上的坑
链接:https://blog.csdn.net/mgdj25/article/details/104815526