springboot批量读取参数映射到实体类

spring读取配置参数可以通过${name}的方式获取,如properties文件中存在如下配置

person.username=xi

则可通过${person.username}获取其对应的值xi

@value("${person.username}")
private String name;

但如果参数过多,在使用这种方式配置,就比较麻烦,springboot中新添加了一个注解@ConfigurationProperties,可以批量将配置参数映射到实体类中。添加依赖,如果不加,代码中会给出黄色提示

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
</dependency>

假设当前存在一个工厂的配置如下

com.demo.factory.name=Furniture Factory
com.demo.factory.addr=bj sy
com.demo.factory.props.work-time=8:00-20:00
com.demo.factory.props.maintenance-time=3 years

com.demo.factory.type[0]=table
com.demo.factory.type[1]=chair
com.demo.factory.type[2]=sofa
com.demo.factory.type[3]=bed

com.demo.factory.desc=${com.demo.factory.name} address: ${com.demo.factory.addr}, working time: ${com.demo.factory.props.work-time}, product type includ: ${com.demo.factory.type[1]} 

其中,desc中使用了当前配置文件中的参数,props可以看成是一个map,type是一个list集合

对应的实体类为

package com.springbootdemo;

import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("com.demo.factory")
public class Factory {

    private String name;
    private String addr;
    private Map<String, String> props;
    private List<String> type;
    private String desc;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public Map<String, String> getProps() {
        return props;
    }

    public void setProps(Map<String, String> props) {
        this.props = props;
    }

    public List<String> getType() {
        return type;
    }

    public void setType(List<String> type) {
        this.type = type;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "Factory [name=" + name + ", addr=" + addr + ", props=" + props + ", type=" + type + "]";
    }

}

测试,实现CommandLineRunner 的类,被纳入到spring管理后,程序启动之后就会执行该类中的run方法

package com.springbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MainBusi implements CommandLineRunner {

    @Autowired
    private Factory factory;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(factory);
        System.out.println(factory.getDesc());
    }

}

上述两行打印输出的结果如下

Factory [name=Furniture Factory, addr=bj sy, props={work-time=8:00-20:00, maintenance-time=3 years}, type=[table, chair, sofa, bed]]
Furniture Factory address: bj sy, working time: 8:00-20:00, product type includ: chair 

关于配置文件中参数对应到实体类中的属性,springboot有如下规则可参考

 

posted @ 2019-09-15 18:48  大坑水滴  阅读(4744)  评论(0编辑  收藏  举报