springboot读取xml配置文件

xml文件内容

在src/main/resources下面添加《operator-config.xml》文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<operator-config group="business">
    <operator-table code="tb_20200401">
        <name>测试0401</name>
    </operator-table>
    <operator-table code="tb_20200509">
        <name>测试0509</name>
    </operator-table>
</operator-config>

添加实体类

解析xml文件,需要根据xml的内容创建实体类,用于存放读取到的xml文件的内容

添加OperatorTable实体类

用于存储<operator-table>对应的数据,代码如下:

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAttribute;

public class OperatorTable implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -3462638489558342948L;

    private String code;

    private String name;

    @XmlAttribute
    public String getCode() {
        return code;
    }


    public void setCode(String code) {
        this.code = code;
    }


    public String getName() {
        return name;
    }


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

}

添加OperatorConfig实体类

用于存放<operator-config>对应的数据,代码如下:

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang3.StringUtils;

@XmlRootElement(name = "operator-config")
public class OperatorConfig {

    private String group;

    private List<OperatorTable> operatorTableList = new ArrayList<OperatorTable>();

    public boolean containsOperatorTable(String tableCode) {
        if (StringUtils.isEmpty(tableCode)) {
            return false;
        }
        for (int i = 0; i < operatorTableList.size(); i++) {
            OperatorTable operatorTable = operatorTableList.get(i);
            if (tableCode.equalsIgnoreCase(operatorTable.getCode())) {
                return true;
            }
        }
        return false;
    }

    @XmlAttribute
    public String getGroup() {
        return group;
    }


    public void setGroup(String group) {
        this.group = group;
    }


    @XmlElement(name="operator-table")
    public List<OperatorTable> getOperatorTableList() {
        return operatorTableList;
    }


    public void setOperatorTableList(List<OperatorTable> operatorTableList) {
        this.operatorTableList = operatorTableList;
    }

}

注解说明

@XmlRootElement(name = “operator-config”):根节点说明,必须要有,否则读取不到数据
@XmlAttribute:标识出该变量对应的是xml中的属性
@XmlElement(name=“operator-table”):标识出该变量对应的子节点

读取XML文件类

用于读取xml文件,代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OperatorConfigManager {

    /**
     * Logger
     */
    private final Logger log = LoggerFactory.getLogger(OperatorConfigManager.class);

    /**
     * 加载的OperatorConfig
     */
    private OperatorConfig operatorConfig = null;

    /**
     * 内部单例实例
     */
    private static OperatorConfigManager instance = new OperatorConfigManager();

    /**
     * 取得OperatorConfig加载器实例。
     *
     * @return OperatorConfig加载器实例
     */
    public static OperatorConfigManager getInstance() {
        return instance;
    }

    /**
     * 构造方法。
     */
    private OperatorConfigManager() {
        if (this.operatorConfig == null) {
            this.operatorConfig = loadOperatorConfig();
        }
    }

    /**
     * GETTER(operatorConfig)
     *
     * @return operatorConfig
     */
    public OperatorConfig getOperatorConfig() {
        return operatorConfig;
    }

    /**
     * 从OperatorConfig定义文件中加载OperatorConfig。
     *
     * @param defineInput 定义文件输入流
     * @return OperatorConfig
     */
    public OperatorConfig reloadOperatorConfig(InputStream defineInput) {
        JAXBContext jc;
        OperatorConfig operatorConfig = null;
        try {
            jc = JAXBContext.newInstance(OperatorConfig.class);
            Unmarshaller u = jc.createUnmarshaller();
            operatorConfig = (OperatorConfig) u.unmarshal(defineInput);
        } catch (JAXBException e) {
            log.error("加载配置文件operator-config.xml出错", e);
        }
        return operatorConfig;
    }

    /**
     * 从默认的OperatorConfig定义文件(classpath://operator-config.xml)中加载OperatorConfig。
     *
     * @return OperatorConfig
     */
    @SuppressWarnings("resource")
    private OperatorConfig loadOperatorConfig() {
        InputStream is = null;
        try {
            String outpath = System.getProperty("user.dir") + File.separator;// 先读取外部目录的,没有再加载classpath的
            is = new FileInputStream(new File(outpath + "operator-config.xml"));
        } catch (IOException e) {
            System.out.println(e.getMessage());
            is = OperatorConfigManager.class.getClassLoader().getResourceAsStream("operator-config.xml"); // 默认加载classpath的
        }
        if (is != null) {
            return reloadOperatorConfig(is);
        } else {
            return null;
        }
    }
}

loadOperatorConfig()方法中,会优先读取编译成jar包后外面的xml文件,读取不到,会读取打在jar包里面的xml文件

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值