SpringBoot入门篇———yaml与properties配置文件

springboot支持.properties,.yaml等配置文件,xml当然也可以,因为传统的spring和其他大量项目都广泛使用xml做配置文件,如maven.但yaml更简洁和高效.下面几种配置文件的运用例子

properties配置文件

如在application.properties中配置端口号和项目的访问路径

server.servlet.context-path=/Test
server.port=10086

此时需要访问http://localhost:10086/Test/test

yaml配置文件

YAML是一个可读性高,用来表达数据序列化的格式.YAML是"YAML Ain’t a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。yaml比xml更简洁,支持json所不支持的注释功能,可读性更好,更适合做配置文件.

yaml语法格式

  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许空格 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释

YAML 支持纯量(字符串,布尔值,整数,浮点数,Null,时间与日期),对象,数组(list,set等)等.
如一个数组 hobbies,数组里面的每一个item缩进后用一个短横线+空格+具体值表示.注意空格.没有会报错.其实使用IDEA写对了就是蓝色,另外数组每一项写对了回车后下一行就自动出现一个短横线.

 hobbies:
    - writing
    - programming
    - wathing films

当然,数组也可以写在一行,也就是所谓的行内写法:

 hobbies: [writing,programming,wathing films]

下面给出使用yaml的例子,通过yaml配置文件完成实体属性值的绑定注入功能
1.建立两个实体类:

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;

/**
 * 学生
 * @author XiaoXin
 * @date 2020/2/16 下午6:08
 */
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private String name;
    private Integer age;
    private Date birthday;
    private boolean hasCar;
    private String[] hobbies;
    private Map<Object,Object> map;
    private List<Object> list;
    private MyClass myClass;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("student setName() is called");
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isHasCar() {
        return hasCar;
    }

    public void setHasCar(boolean hasCar) {
        this.hasCar = hasCar;
    }

    public String[] getHobbies() {
        return hobbies;
    }

    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }

    public Map<Object, Object> getMap() {
        return map;
    }

    public void setMap(Map<Object, Object> map) {
        this.map = map;
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

    public MyClass getMyClass() {
        return myClass;
    }

    public void setMyClass(MyClass myClass) {
        this.myClass = myClass;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", hasCar=" + hasCar +
                ", hobbies=" + Arrays.toString(hobbies) +
                ", map=" + map +
                ", list=" + list +
                ", myClass=" + myClass +
                '}';
    }
}
**
 * 班级
 * @author XiaoXin
 * @date 2020/2/16 下午6:12
 */
public class MyClass {
    private Integer cid;
    private String cname;
    private Integer num;

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
        System.out.println("myclass setCid() is called");
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
        System.out.println("myclass setCName() is called");
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "MyClass{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                ", num=" + num +
                '}';
    }
}

2.application.yaml配置如下:

student:
  name: ShawHsin
  age: 20
  hasCar: true
  birthday: 1998/05/13
  hobbies:
    - writing
    - programming
    - wathing films
  map: {k1:v1,k2:v2}
  list:
    - 10
    - 20
    - 30
  myClass:
    cname: 计科一班
    cid: 1
    num: 20

3.在test下进行测试:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    Student student;//加入到容器
    @Test
    void contextLoads() {
        System.out.println("student:"+student);//获取
    }

}

4.运行结果:
myclass setCid() is called

myclass setCName() is called
student setName() is called
2020-02-16 19:20:01.340  INFO 13722 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-16 19:20:01.639  INFO 13722 --- [        `在这里插入代码片`   main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 2.509 seconds (JVM running for 3.837)

student:Student{name='ShawHsin', age=20, birthday=Wed May 13 00:00:00 CST 1998, hasCar=true, hobbies=[writing, programming, wathing films], map={k1v1=, k2v2=}, list=[20, 30], myClass=MyClass{cid=1, cname='计科一班', num=20}}

我们可以看到,学生属性值完成了注入,可以看到,相关set方法被调用,明显运用了反射读取yaml文件,通过类属性名与yaml所配置的信息名映射,完成属性的赋值.注意,Student 类加了两个必须注解@Component 和@ConfigurationProperties(prefix = “student”),去掉component,无法编译,去掉ConfigurationProperties注解,属性值无法注入,为null.当然,如果相关类不提供set方法,值也为null.当yaml配置了很多信息时,ConfigurationProperties(prefix = “student”)的prefix表明使用该文件内的那个信息.我们注入Student student,就把yaml的student给它.如果我们注入Student csdn_student,而yaml使用student.,相关属性的值会注入吗?会的

	@Autowired
    Student csdn_student;
    @Test
    void contextLoads() {
        System.out.println("csdn_student:"+csdn_student);
    }
csdn_student:Student{name='ShawHsin', age=20, birthday=Wed May 13 00:00:00 CST 1998, hasCar=true, hobbies=[writing, programming, wathing films], map={k1v1=, k2v2=}, list=[20, 30], myClass=MyClass{cid=1, cname='计科一班', num=20}}

因为Student类的注解@ConfigurationProperties(prefix = “student”),若prefix =“csdn_student”,直接无法编译.若prefix =“csdnstudent” ,结果为null,因为yaml里面没有前缀对应的信息
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值