yml的基本语法,以及java读取yml文件

目录

 

spring Boot层级结构

基本概念

演示


 

spring Boot层级结构

static:保存所有的静态资源:js css images;

templates:保存所有模板页面;(Spring Boot默认jar包使用嵌入式Tomcat,默认不支持JSP页面);可以使用模版引擎(freemarker、thymeleaf)

application.properties:Spring Boot应用的配置文件

 

 

基本概念

Spring Boot使用一个全局的配置文件

application.properties

application.yml

 

配置文件放在src/main/resources目录或者类路径/config下

yml是YAML(YAML Ain't Markup Language)语言文件,以数据为中心,比json、xml等更加合适。

 

如下:

yml文件:

server:
  port: 8082

properties文件:

server.port=8081

YAML基本语法:

使用缩进表示层级关系;

缩进时不运行使用Tab,只允许使用空格。

和Python一样!

大小写敏感;

 

YAML支持三种数据结构:

对象:键值的集合

数组:一组按次序排列的值

字面量:单个的、不可再分的值

 

双引号:"zhangsan \n list":输出zhangsan 换行 list

单引号:'zhangsan \n list':输出zhangsan \n list

 

下面给出一个例子:

server:
  port: 8082


person:
  lastName: hello
  age: 18
  boss: false
  birth: 2018/3/3
  maps: {k1: v1, k2: 12}
  lists:
    - lisi
    - zhaoliu

  dog:
    name: 小白
    age: 5

 

 

演示

如下目录结构:

yml的基本语法,以及java读取yml文件_第1张图片

Dog.java

package com.analyze.demo.bean;

public class Dog {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

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

Person.java

package com.analyze.demo.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    private Date birth;

    private Map maps;
    private List lists;
    private Dog dog;

}
 
  

application.yml

server:
  port: 8082


person:
  lastName: hello
  age: 18
  boss: false
  birth: 2018/3/3
  maps: {k1: v1, k2: 12}
  lists:
    - lisi
    - zhaoliu

  dog:
    name: 小白
    age: 5

DemoApplicationTests

package com.analyze.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.analyze.demo.bean.Person;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

	@Autowired
	Person person;

	@Test
	public void contextLoads() {

		System.out.println(person);
	}

}

运行截图如下:

yml的基本语法,以及java读取yml文件_第2张图片

你可能感兴趣的:(Java,Spring,Boot)