[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;
}
}
<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;
}
}