Spring-boot-study

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;
    }
}

注解详解:

  1. @ConfigurationProperties(prefix = “emp”)将该类中的所有属性和配置文件中相关配置绑定
  2. @Component 解决@ConfigurationProperties注解报错 , 将当前组件作为spring boot中一个组件来使用,才能纳入spring boot的管理
  3. @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);
    }
    
}

结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值