企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**1.application.yml** ~~~ #配置user对象,包含name和age两个属性 user: name: tom age: 18 #user2 行内写法 user2: {name: jerry, age: 15} #list | set | 数组 list1: - tom - jerry - jack - rose #list 行内写法 list2: [tom, jerry, jack, rose] person: firstName: tom lastName: cat age: 18 married: false birthday: 1991/03/19 map: {key1: v1, key2: v2} list: - tom - jerry - jack - rose set: - name: hanmeimei age: 18 - name: hanmeimei age: 18 user: name: nobb age: 25 ~~~ **2.属性自动注入** ~~~ package com.nobb.domain; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; @Component //自动属性注入,需要set方法 @ConfigurationProperties(prefix = "person") public class Person { // @Value("${person.firstName}") private String firstName; // @Value("${person.lastName}") private String lastName; private Integer age; private boolean married; private Date birthday; private Map map; private List<String> list; private Set<User> set; private User user; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public boolean isMarried() { return married; } public void setMarried(boolean married) { this.married = married; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<User> getSet() { return set; } public void setSet(Set<User> set) { this.set = set; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", age=" + age + ", married=" + married + ", birthday=" + birthday + ", map=" + map + ", list=" + list + ", set=" + set + ", user=" + user + '}'; } } ~~~