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

歡迎訪問 生活随笔!

生活随笔

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

java

yaml格式,给Java类绑定数据

發布時間:2023/12/10 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 yaml格式,给Java类绑定数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里寫目錄標題

    • 1、基本語法
    • 2、給java bean注入值
    • 3、測試

1、基本語法

# yaml 配置文件寫法,代替properties寫法 # 嚴格區分空格# 內注入到配置類中 server:port: 8081# 對象 student:name: jackage: 3# 行內寫法 map student1: {name: jack, age: 3}# array or collections pets:- dog- cat- turtlepets1: [dog, cat, turtle]# 還可以寫隨機數 或者 uuid uuid: ${random.uuid} random: ${random.int}

2、給java bean注入值

  • 核心
@ConfigurationProperties(prefix = "person") // yaml 里面寫person的屬性 // 應用:數據源配置..
  • application.yaml
# yaml 對象注入 dog:name: 八公age: 6person:person-name: abc@163.com # bitqian_${random.uuid}happy: falsemaps: {k1: v1, k2: v2}age: ${random.int}hobbyList:- girl- code- bookdog:name: 拉布拉多age: 3server:port:8081# https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
  • 這里以dog,和person實體類對各種數據類型說明
  • dog 這里用了properties加載配置,官方推薦ymal
package cn.bitqian.entity;import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;/*** @author echo lovely* @date 2020/9/26 21:45*/ @Component // @ConfigurationProperties(prefix = "dog") @PropertySource(value = "classpath:application.properties") public class Dog {// value & SPEL @Value("${server.port}")private String name;private Integer age;public Dog() {}public Dog(String name, Integer age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +'}';} }
  • person類
package cn.bitqian.entity;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated;import javax.validation.constraints.Email;import java.util.List; import java.util.Map;/*** @author echo lovely* @date 2020/9/26 21:46*/ @Component // 對應前綴 @ConfigurationProperties(prefix = "person") // setter/getter 與yaml對應 @Validated // 數據校驗 public class Person {@Email(message = "必須是郵箱格式")private String personName;private Boolean happy;private Integer age;private Map<String, Object> maps;private List<Object> hobbyList;private Dog dog;public Person() {}public Person(String name, Boolean happy, Integer age,Map<String, Object> maps, List<Object> hobbyList, Dog dog) {this.personName = name;this.happy = happy;this.age = age;this.maps = maps;this.hobbyList = hobbyList;this.dog = dog;}public String getPersonName() {return personName;}public void setPersonName(String personName) {this.personName = personName;}public Boolean getHappy() {return happy;}public void setHappy(Boolean happy) {this.happy = happy;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getHobbyList() {return hobbyList;}public void setHobbyList(List<Object> hobbyList) {this.hobbyList = hobbyList;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}@Overridepublic String toString() {return "Person{" +"personName='" + personName + '\'' +", happy=" + happy +", age=" + age +", maps=" + maps +", hobbyList=" + hobbyList +", dog=" + dog +'}';} }

3、測試

package cn.bitqian;import cn.bitqian.entity.Dog; import cn.bitqian.entity.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;@SpringBootTest class Springboot02ApplicationTests {@Resourceprivate Dog dog;@Autowired@Qualifier(value = "person")private Person person;@Testvoid contextLoads() {System.out.println(dog);System.out.println(person);}}

age是隨機的int,personName 做了郵箱驗證

總結

以上是生活随笔為你收集整理的yaml格式,给Java类绑定数据的全部內容,希望文章能夠幫你解決所遇到的問題。

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