SpringBoot——yml配置文件多数据类型自定义Bean注入容器

1.pom.xml

        <!--web项目jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--set、get、tostring、slf4j日志、无参有参--> 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--此包为,自动提示。为了让自定义的组件放入容器,在配置文件yml中使用的时候提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

2.bean

@ConfigurationProperties (prefix = "person")
@Component
@Data
public class Person {
    private String userName;            //字符串
    private Boolean boss;               //布尔
    private Date birth;                 //日期
    private Integer age;                //整型
    private Pet pet;                    //自定义对象
    private String[] interests;         //字符串数组
    private List<String> animal;        //链表泛型字符串
    private Map<String, Object> score;  //map双列集合泛型(字符串,对象)
    private Set<Double> salarys;        //set单列集合泛型双精度
    private Map<String, List<Pet>> allPets;  //map双列集合泛型(字符串,链表泛型自定义对象)
}

@Data
public class Pet {
    private String name;
    private Double weight;
}

@ConfigurationProperties:扫描配置文件前缀prefix

@Component:声明该类为一个组件

3.application.yml

person:
  username: "zhangsan \n lisi" #从yml层面看,单引号转义,双引号不转义
  boss: true
  birth: 2019/09/06
  age: 18
  pet: {name: 大黄,weigth: 66.3}
  interests: [篮球,足球,排球]
  animal: [大华,大白,大闲]
  score: {xx: 99,xxx: 88} #key不能是中文
  salarys: [888.22,55.55,66.55]
  allPets:
    sick: [{name: 大黄,weight: 66.3},{name: 大黑,weight: 66.33}]
    health: [{name: 大白, weight: 66.33},{name: 大胖,weight: 66.33}]

4.controller

@RestController
public class TestController {

    @Autowired
    Person person;

    @RequestMapping("person")
    public Person getPersonDate(){
        return person;
    }
}

@RestController:复合注解,@Controller和@ResponseBody封装

@Autowired:注入容器中的组件

5.psotman测试接口

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值