生活随笔
收集整理的這篇文章主要介紹了
yaml格式,给Java类绑定数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這里寫目錄標題
- 1、基本語法
- 2、給java bean注入值
- 3、測試
1、基本語法
server:port: 8081
student:name: jack
age: 3
student1: {name: jack
, age: 3}
pets:- dog
- cat
- turtle
pets1: [dog
, cat
, turtle
]
uuid: $
{random.uuid
}
random: $
{random.int
}
2、給java bean注入值
@ConfigurationProperties(prefix = "person")
// yaml 里面寫person的屬性
// 應用:數(shù)據(jù)源配置..
dog:name: 八公
age: 6person:person-name: abc@163.com
happy: falsemaps: {k1: v1
, k2: v2
}age: $
{random.int
}hobbyList:- girl
- code
- book
dog:name: 拉布拉多
age: 3server:port:8081
- 這里以dog,和person實體類對各種數(shù)據(jù)類型說明
- 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
;
@Component
@PropertySource(value
= "classpath:application.properties")
public class Dog {@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
+'}';}
}
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
;
@Component
@ConfigurationProperties(prefix
= "person")
@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类绑定数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。