SpringBoot 学习笔记(二)yaml配置文件

一、yaml语法

#yaml
	#对空格要求十分严格
	
	#普通的key-value
	#格式:key:空格 value
	name: yanao
	
	#对象
	student:
	  name: yanao
	  age: 3
	
	#行内写法
	student2: {name: yanao,age: 3}
	
	#数组
	pets:
	  - cat
	  - dog
	  - pig
	
	pets2: [cat,dog,pig]

二、yaml使用流程

  1. 注解person类
@Component
@ConfigurationProperties(prefix="person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
  1. application.yaml配置文件
person:
  name: yanao
  age: ${random.int}
  happy: false
  birth: 2019/07/27
  maps: {m1: v1,m2: v2}
  lists: [code,music,girl]
  dog:
    name: lipu
    age: 3
  1. 调用yaml
@Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }

三、4个位置写配置文件

  1. 按默认优先级排序
 1. file:./config/
 2. file:./
 3. classpath:/config/
 4. classpath:/

2.自主选择环境 application.yaml

 server:
  port: 8081
spring:
  profiles:
    active: test #用active进行选择
    
#用---分隔   
---
server:
  port: 8082
spring:
  profiles: dev

---
server:
  port: 8083
spring:
  profiles: test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值