Spring启动加载解析xml的key-value值

1,bean配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<!-- 启动加载 -->
<bean id="springLoadInit" class="启动加载的java类路径"
  init-method=" init 为加载的方法名">
<property name="filePath" value="conf/init_value.xml" />
    </bean>

2,init_value.xml初始值存放的位置
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<company key="zzelectric" value="01" />
<company key="kfelectric" value="02" />
</mapping>

3,初始化java类的处理
public class SpringLoadInit {
  private String filePath;
public void init() {
parseXml();
}
public void parseXml() {
SAXReader reader = new SAXReader();

org.dom4j.Document doc = null;
try {
doc = reader.read(SpringLoadInit.class.getClassLoader()
.getResourceAsStream(this.getFilePath()));
} catch (DocumentException e) {
System.out.println("加载初始化文件出错" + e);
}
Element root = doc.getRootElement();
List<Element> eleList = root.selectNodes("/mapping/company");
for (Element e : eleList) {// 循环读取每个节点
String key = e.attributeValue("key");
String value = e.attributeValue("value");
System.out.println(key+value);
}
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}

public String getFilePath() {
return filePath;
}
}

你可能感兴趣的:(key-value)