标签:lis yml href object bool private 数据 空格 depend
//示例:
server:
port: 8081 //此处有空格
// 编写application.yml配置文件
server:
port: 8081
person:
lastName: zhangsan
age: 18
boss: false
birth: 2008/8/8
maps: {k1: v1, k2: v2}
lists:
- zhaoliu
- lisi
dog:
name: 鲁亚
age: 12
// 实体类
public class Dog{
private String name;
private Integer age;
// 省略 getter,setter,toString
}
/**
* @ConfigurationProperties 将本类中的所有属性和配置文件中相关配置进行绑定
* prefix = "person": 配置文件中哪个下面的所有属性进行一一映射;
*
* @Component
* 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能;
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person{
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
// 省略 getter,setter,toString
}
// pom.xml 中导入配置文件处理器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
properties
配置文件# application.properties
# 配置person的值
person.lastName=张三
person.age=18
person.birth=2018/5/3
person.boss=false
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=xiaohua
person.dog.age=11
参考资料:
标签:lis yml href object bool private 数据 空格 depend
原文地址:https://www.cnblogs.com/linkworld/p/9131639.html