sun java学习_Java学习笔记 -- yaml文件配置

yaml文件语法:

a4bf34e2ccb4e562df0a117db205bbce.png

5978b6e5e10e11ee1f5e4d7f301b9514.png

----------------------------实际操作---------------------------------

文件目录:

1778aa103db22c829126a91d374c8e74.png

创建Cat类:

package com.springbootpractise.pojo;public classCat {privateString name;privateInteger age;publicCat() {

}publicCat(String name, Integer age) {this.name =name;this.age =age;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicInteger getAge() {returnage;

}public voidsetAge(Integer age) {this.age =age;

}

@OverridepublicString toString() {return "Cat{" +

"name='" + name + '\'' +

", age=" + age +

'}';

}

}

创建Person类:

package com.springbootpractise.pojo;

import org.springframework.beans.factory.annotation.Value;

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

import org.springframework.context.annotation.PropertySource;

import org.springframework.stereotype.Component;

import java.util.Date;

import java.util.List;

import java.util.Map;

@Component

@ConfigurationProperties(prefix="person")//加载指定配置文件//@PropertySource(value="classpath:qinjiang.properties")

public classPerson {//SPEL表达式取出配置文件的值//@Value("${name}")

privateString name;privateInteger age;privateBoolean happy;privateDate birth;private Mapmaps;private Listlists;privateCat cat;publicPerson() {

}public Person(String name, Integer age, Boolean happy, Date birth, Map maps, Listlists, Cat cat) {this.name =name;this.age =age;this.happy =happy;this.birth =birth;this.maps =maps;this.lists =lists;this.cat =cat;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicInteger getAge() {returnage;

}public voidsetAge(Integer age) {this.age =age;

}publicBoolean getHappy() {returnhappy;

}public voidsetHappy(Boolean happy) {this.happy =happy;

}publicDate getBirth() {returnbirth;

}public voidsetBirth(Date birth) {this.birth =birth;

}public MapgetMaps() {returnmaps;

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

}public ListgetLists() {returnlists;

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

}publicCat getCat() {returncat;

}public voidsetCat(Cat cat) {this.cat =cat;

}

@OverridepublicString toString() {return "Person{" +

"name='" + name + '\'' +

", age=" + age +

", happy=" + happy +

", birth=" + birth +

", maps=" + maps +

", lists=" + lists +

", cat=" + cat +

'}';

}

}

yaml文件配置内容:

person:

name: sunice

age:3happy:falsebirth:2021/02/16maps: {k1: v1,k2: v2}

lists: [1,2,3,4,5,cat]

cat: {name:"猫",age: 3}

Test文件:

package com.springbootpractise;

import com.springbootpractise.pojo.Person;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTestclassSpringboot02ConfigApplicationTests {

@AutowiredprivatePerson person; //配置Person类

@TestvoidcontextLoads() {

System.out.println(person); //打印Person类

} }

运行结果:

Person{name='sunice', age=3, happy=false, birth=Tue Feb 16 00:00:00 CST 2021, maps={k1=v1, k2=v2}, lists=[1, 2, 3, 4, 5, cat], cat=Cat{name='猫', age=3}}

备注:

使用 @ConfigurationProperties(prefix="person") 后,IDEA会变红,在pom.xml中添加依赖项即可解决问题

org.springframework.boot

spring-boot-configuration-processor

true

----------------- yaml文件占位符----------------------------------------

yaml文件中,也可以使用表达式进行赋值。${XXX}  是yaml文件中的占位符。XXX可以填写表达式。

person:

name: sunice${random.uuid}

age:3happy:falsebirth:2021/02/16maps: {k1: v1,k2: v2}

lists: [1,2,3,4,5,cat]

hello: world

cat:

name: ${person.hello:hello}_猫

运行结果:

Person{name='sunice115c27cd-44e9-406c-87b3-ca98a4860974', age=3, happy=false, birth=Tue Feb 16 00:00:00 CST 2021, maps={k1=v1, k2=v2}, lists=[1, 2, 3, 4, 5, cat], cat=Cat{name='world_猫', age=null}}

---------------yaml文件松散绑定----------中划线和驼峰命名可以互相映射---------

8684d6989e3ccc7d597b7c4fd14a89ca.png

c46bc2cde00d49096b2df1eb8676ce49.png

运行结果:

df0afcec319e96ca76cc7614619291b8.png

240c18ddeac4eec849553b7b31fc3c79.png

参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值