首先创建两个类,Person和Dog。为了可以被扫描到,在前面加入@Component注解。
Person类如下:
package jiang.com.helloworld.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
public class Person {
private String name;
private int age;
private boolean happy;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
public Person(String name, int age, boolean happy, Date birthday, Map<String, Object> maps, List<Object> lists) {
this.name = name;
this.age = age;
this.happy = happy;
this.birthday = birthday;
this.maps = maps;
this.lists = lists;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHappy() {
return happy;
}
public void setHappy(boolean happy) {
this.happy = happy;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birthday=" + birthday +
", maps=" + maps +
", lists=" + lists +
'}';
}
}
Dog类如下:
package jiang.com.helloworld.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
public class Dog {
private String name;
private Integer age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
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;
}
public Dog() {
}
}
其次,在application.yaml配置文件中填入属性。
person:
name: wangcai${random.uuid} #wangcai与随机的字符串拼接
age: ${random.int}
happy: true
birth: 1966/6/16
maps: {k1: v1,k2: v2}
lists:
- cats
- dogs
- pigs
dogs:
name: wbq${person.hello:hello}_wangcai #如果person.hello存在则wbq与person.hello拼接,如果不存在则与hello拼接。
age: 3
然后,在实体类另外加上@ConfigurationProperties(prefix = "")。其中,prefix后面对应的是配置文件中的类名。
Person类:
package jiang.com.helloworld.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
private boolean happy;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
public Person(String name, int age, boolean happy, Date birthday, Map<String, Object> maps, List<Object> lists) {
this.name = name;
this.age = age;
this.happy = happy;
this.birthday = birthday;
this.maps = maps;
this.lists = lists;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHappy() {
return happy;
}
public void setHappy(boolean happy) {
this.happy = happy;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birthday=" + birthday +
", maps=" + maps +
", lists=" + lists +
'}';
}
}
Dog类:
package jiang.com.helloworld.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "dogs")
public class Dog {
private String name;
private Integer age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
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;
}
public Dog() {
}
}
之后编写测试代码。记得在每个类上面加自动注入的注解@Autowired。
package jiang.com.helloworld;
import jiang.com.helloworld.pojo.Dog;
import jiang.com.helloworld.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HelloworldApplicationTests {
@Autowired
private Person person;
@Autowired
private Dog dog;
@Test
void contextLoads() {
System.out.println(person);
System.out.println(dog);
}
}
输出结果如下:
Person{name='wangcaicc2668d4-0a56-4adb-9ef3-a82f81428430', age=-1447258931, happy=true, birthday=null, maps={k1=v1, k2=v2}, lists=[cats, dogs, pigs]}
Dog{name='wbqhello_wangcai', age=3}
注意!如果熟悉名是驼峰的方式,如myDog,则可以在配置文件中用my-dog替换。
如创建了一个Cat类:
package jiang.com.helloworld.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "cat")
public class Cat {
private String catName;
private Integer catAge;
@Override
public String toString() {
return "Cat{" +
"catName='" + catName + '\'' +
", catAge=" + catAge +
'}';
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
public Integer getCatAge() {
return catAge;
}
public void setCatAge(Integer catAge) {
this.catAge = catAge;
}
public Cat(String catName, Integer catAge) {
this.catName = catName;
this.catAge = catAge;
}
public Cat() {
}
}
配置文件可以写成:
cat:
cat-name: wbq
cat-age: 20
测试代码:
package jiang.com.helloworld;
import jiang.com.helloworld.pojo.Cat;
import jiang.com.helloworld.pojo.Dog;
import jiang.com.helloworld.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HelloworldApplicationTests {
@Autowired
private Cat cat;
@Test
void contextLoads() {
System.out.println(cat);
}
}
测试结果: