yml的使用总结

1. 通用写法

  • 示例如下:
# 变量名 然后换行缩进(不能使用Tab键缩进),子变量名,然后英文冒号: 然后 空格 然后 值
server: 
	port: 8080

address: 
	province: gd
	city: dg
	town: qx
	detail: xxx路xxx号	

# 数组的写法
cards: 
	- 1001
	- 1002
	- 1003

2. 对象的行内写法

# key: value之间的空格不能省略,对象行内写法大括号必须有
user: {id: 1001,name: tom,age: 18}

3. 数组的行内写法

# 数组必须用中括号括起来
cards: [1001,1002,1003]

4. 纯量

# 单引号不能识别转移字符, \n会直接输出 \n
msg1: 'hello \n world'

# 双引号可以识别转移字符, 对应的值会分两行输出
msg2: "hello \n world"

5. 参数引用

name: tom
age: 18
id: 1001

# person对象引用上面三个参数的值,作为自己的属性的值
person: 
	id: {id}
	name: ${name}
	age: ${age}

6. yml书写总结

yml区分大小写

yml的值前面必须有空格,用于分割

空格表示缩进,缩进的空格数相同表示同级

7. 获取yml配置文件的数据

方式1 @Value("${变量名}")

  • @Value("${变量名}")
  • Java类中定义的变量名可以和yml声明的变量名不同名
  • 但是注解中的变量名必须和yml中声明的变量名同名
name: tom
arr01: 
	- 1001
	- 1002
person: 
	name: jerry
	age: 18
package com.lchh.lt.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloConfig {

    @Value("${name}")
    private String ymlName;
	
	@Autowired
	private Environment env;
	
    @RequestMapping("/hello11")
    public String getYmlName(){
        return "hello "+ymlName;
    }
}

  • 访问浏览器,获取结果
    在这里插入图片描述
  • 可以看出获取到配置文件的变量的值

方式2 Environment env

person: 
	id: 1001
	name: tom 
	age: 18

arr: 
	- 1001
	- 1002
	- 1003	
import org.springframework.beans.factory.annotation.Autowired;

@Autowired
private Environment env;

@RequestMapping("/hello")
public String sayHello(){
	String name = env.getProperty("person.name");
	Integer arr01 = env.getProperty("arr[0]");
	System.out.println(name );
	System.out.println(arr01 );
}

方式3 @ConfigurationProperties

  • 定义实体bean Person
package com.lchh.lt.bean;

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

@Data //自动注入get set  toString 等方法
@ConfigurationProperties
@Component //注入到spring容器
public class Person {
    private Integer id;
    private String name;
    private Integer age;

}
  • 定义yml配置文件
name: tom
person:
  id: 1001
  name: jerry
  age: 18
  • 获取yml文件中的值
package com.lchh.lt.config;

import com.lchh.lt.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloConfig {

    @Autowired
    private Environment environment;

    @Autowired
    private Person person;

    @RequestMapping("/hello")
    public String sayHello(){

        System.out.println("person="+person);

        return "hello";
    }
}
  • 控制台输出结果
    在这里插入图片描述
  • 发现输出的结果并不是我们想要的,这个时候用到一个属性 prefix 前缀
package com.lchh.lt.bean;

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

@Data //自动注入get set  toString 等方法
@ConfigurationProperties(prefix="person")  //带上前缀声明
@Component //注入到spring容器
public class Person {
    private Integer id;
    private String name;
    private Integer age;

}
  • 重新启动,然后开控制台打印结果
    在这里插入图片描述

8. yml配置文件获取注解提示

  • pom.xml文件添加依赖
<!--配置springboot注解提示-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇洋葱头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值