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

[color=brown][size=x-large]1,bean配置[/size][/color]
<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="[color=red]init 为加载的方法名[/color]">
<property name="filePath" value="conf/init_value.xml" />
</bean>

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

[color=brown][size=x-large]3,初始化java类的处理[/size][/color]
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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值