日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2024/3/24 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sun java学习_Java学习笔记 -- yaml文件配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

yaml文件語法:

----------------------------實際操作---------------------------------

文件目錄:

創建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文件松散綁定----------中劃線和駝峰命名可以互相映射---------

運行結果:

參考資料:

總結

以上是生活随笔為你收集整理的sun java学习_Java学习笔记 -- yaml文件配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。