Spring-boot-study01-实体类属性和配置文件绑定
用户类
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;
/**
* 实体类
* @ConfigurationProperties(prefix = "emp")将该类中的所有属性和配置文件中相关配置绑定
* prefix = "emp"将配置文件下的属性和emp类中属性进行一一映射
* @Component 解决@ConfigurationProperties注解报错 , 将当前组件作为spring boot中一个组件来使用,才能纳入spring boot的管理
* @Validated 开启数据校验
* @PropertySource:加载局部配置文件
*/
@PropertySource(value ={ "classpath:emp.properties"} )
//@Validated
@Component
@ConfigurationProperties(prefix = "emp")
public class Emp {
//员工属性
private String name;//姓名
private Integer age;//年龄
private Double sarlay;//工资
private boolean boss;//是否是老板
private Date birthday;//生日
//todo Map list 集合 未知
private Map map;
private List list;
//兴趣的java类 封装兴趣的属性
private Forte forte;
@Override
public String toString() {
return "emp{" +
"name='" + name + '\'' +
", age=" + age +
", sarlay=" + sarlay +
", boss=" + boss +
", birthday=" + birthday +
", map=" + map +
", list=" + list +
", forte=" + forte +
'}';
}
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 Double getSarlay() {
return sarlay;
}
public void setSarlay(Double sarlay) {
this.sarlay = sarlay;
}
public boolean isBoss() {
return boss;
}
public void setBoss(boolean boss) {
this.boss = boss;
}
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 getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Forte getForte() {
return forte;
}
public void setForte(Forte forte) {
this.forte = forte;
}
}
注解详解:
- @ConfigurationProperties(prefix = “emp”)将该类中的所有属性和配置文件中相关配置绑定
- @Component 解决@ConfigurationProperties注解报错 , 将当前组件作为spring boot中一个组件来使用,才能纳入spring boot的管理
- @PropertySource:加载局部配置文件
兴趣类
/**
* 兴趣类
*/
public class Forte {
private String name;//名称
private Integer time;//时长
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getTime() {
return time;
}
public void setTime(Integer time) {
this.time = time;
}
@Override
public String toString() {
return "Forte{" +
"name='" + name + '\'' +
", time=" + time +
'}';
}
}
application.yml配置文件
server:
port: 8088
#emp实体类的配置属性
emp:
name: 赵四
age: 200
sarlay: 1000.0
boss: True
birthday: 0001/01/01
#map 集合
map:
key1: value1
key2: value2
#list 集合
list:
- one
- two
- three
forte:
name: java
time: 1
emp.properties 局部配置文件(防止application.yml中属性臃肿)
#编辑emp中的配置文件
emp.name = 赵四
emp.age = 200
emp.sarlay = 1000.0
emp.boss = false
emp.birthday = 0001/01/01
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
//创建对象
@Autowired//自动注入
Emp emp;
@Test
public void contextLoad(){
System.out.println(emp);
}
}