默认全局配置文件:
application.properties:
- k = v;
application.yml :
k:空格v
yaml ain’t myarkup language(yaml不是一个标记文档,xml是标记文档)
试用 application.yml
创建实体类(給上get set)
public class Student { private String name; private int age; private boolean sex; private Date birthday; private Map location; private List hobbies; private String[] skills; private Pet pet; }
编写application.yml
student: name: zs age: 28 sex: false birthday: 2019/12/24 #时间 location: {province: 新疆,city: 西安} #map hobbies: #list集合 - 足球 - 篮球 skills: # 数组 - 编程 pet: # 特殊对象 pname: 刘波 page: 8
再在实体类加上(注解)
@Component @ConfigurationProperties(prefix="student")
测试类调用
@SpringBootTest class HelloWorldApplicationTests { @Autowired private Student stu; @Test void contextLoads() { System.out.println(stu.toString()); } }
结果: Student [name=zs, age=28, sex=false, birthday=Tue Dec 24 00:00:00 CST 2019, location={province=新疆, city=西安}, hobbies=[足球, 篮球], skills=[编程], pet=Pet [pname=刘波, page=8]]