1.springboot2.X配置文件
a.Springboot的的自动配置是基于约定的(默认端口为8080),可以使用配置文件对默认的配置进行修改;
b.默人的全局配置文件:
application.properties
application.yml:yaml
YAML :YAML不是标记语言,YAML是一个人性化的数据序列化所有编程语言的标准。
语法结构:1.key:空格value;
2.通过垂直对齐,指定层次关系;
3.字符串默认可以不加引号(双引号|单引号),双引号中转义符生效,单引号和不加引号时转义符无效;
4.写法:key-value形式和行内写法;
server:
port: 8882
path: a/b/c
XML:是一个标记文档;<server><port>8882</port><path>a/b/c</path></server>
2.利用配置文件为属性绑定数据(创建类对象,配置文件中配置)
server:
port: 8888
student:
name: amy
age: 19
sex: true
birthday: 2019/02/01
location: {provice: 广东省,city: 广州,zone: 白云区 } #Map行内写法
hobbies:
#- 踢足球
#- 编程
[踢足球,踢足球] #行内写法
skill:
#- java
#- html
[java,html5] #行内写法
pet:
nickName: 旺财
strain: 金毛
package com.example.demo.model;
import javafx.scene.effect.PerspectiveTransform;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component//将javaBean纳入spring管理
@ConfigurationProperties(prefix = "student")//通过该注解识别student类
public class Student {
private String name;
private int age;
private boolean sex;
private Date birthday;
private Map<String,Object> location;
private String[] hobbies;
private List skill;
private Pet pet;
}
package com.example.demo.model;
public class Pet {
private String nickName;
private String strain;
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
Student student;
@Test
public void contextLoads() {
System.out.print(student);
}
}