springboot读取xml配置⽂件xml⽂件内容
在src/main/resources下⾯添加《l》⽂件,⽂件内容如下:
<?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;
l.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) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
添加OperatorConfig实体类
⽤于存放<operator-config>对应的数据,代码如下:
import java.util.ArrayList;
import java.util.List;
l.bind.annotation.XmlAttribute;
l.bind.annotation.XmlElement;
l.bind.annotation.XmlRootElement;
import org.apachemons.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 = (i);
if (tableCode.Code())) {
return true;
}
}
return false;
}
@XmlAttribute
public String getGroup() {
return group;
}
public void setGroup(String 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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
l.bind.JAXBContext;
l.bind.JAXBException;
l.bind.Unmarshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OperatorConfigManager {
/**
* Logger
*/
private final Logger log = Logger(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 {
lol英文名字大全try {
jc = wInstance(OperatorConfig.class);
Unmarshaller u = jc.createUnmarshaller();
operatorConfig = (OperatorConfig) u.unmarshal(defineInput);
} catch (JAXBException e) {
<("加载配置⽂件l出错", e);
}
return operatorConfig;
}
/**
* 从默认的OperatorConfig定义⽂件(classpath://l)中加载OperatorConfig。
*
* @return OperatorConfig
*/
@SuppressWarnings("resource")
private OperatorConfig loadOperatorConfig() {
InputStream is = null;
try {
String outpath = Property("user.dir") + File.separator;// 先读取外部⽬录的,没有再加载classpath的
is = new FileInputStream(new File(outpath + "l"));
} catch (IOException e) {
System.out.Message());
is = ClassLoader().getResourceAsStream("l"); // 默认加载classpath的
}
if (is != null) {
return reloadOperatorConfig(is);
} else {
猜灯谜中秋节return null;
}
}
}
loadOperatorConfig()⽅法中,会优先读取编译成jar包后外⾯的xml⽂件,读取不到,会读取打在jar包⾥⾯的xml⽂件
发布评论