首先是this.getClass().getClassLoader().getResourceAsStream()的问题。
1. java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at com.classLoader.resource.BinaryServiceManager.initialMapping(BinaryServiceManager.java:39)
at com.classLoader.resource.BinaryServiceManager.refresh(BinaryServiceManager.java:68)
at com.classLoader.resource.BinaryServiceServlet.doGet(BinaryServiceServlet.java:10)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
at java.lang.Thread.run(Thread.java:595)
原因: 没有导入jaxen包,一般情况下,dom4j-1.6.1.jar和jaxen-1.1.1.jar要同时使用,后者是前者的基础包。
2, 源代码:
package com.classLoader.resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class BinaryServiceManager {
private HashMap<String, String> binaryMap = null;
private static BinaryServiceManager instance = null;
private BinaryServiceManager() {}
public static BinaryServiceManager getInstance() {
if (instance == null) {
instance = new BinaryServiceManager();
}
return instance;
}
private void initialMapping() {
HashMap<String, String> tempBinaryMap = new HashMap<String, String>();
Document doc = null;
try {
SAXReader reader = new SAXReader();
doc = reader.read(this.getClass().getClassLoader().getResourceAsStream("BinaryServiceMapping.xml"));
System.out.println(this.getClass().getClassLoader().getResourceAsStream("BinaryServiceMapping.xml"));
List binaryServiceList = doc.selectNodes("/binaryServiceMapping/binaryService");
Node binaryServiceInfo = null;
for (int i = 0; i < binaryServiceList.size(); i++) {
binaryServiceInfo = (Node) binaryServiceList.get(i);
String shortCode = binaryServiceInfo.selectSingleNode("./shortCode").getText();
List serviceList = binaryServiceInfo.selectNodes("./services/service");
List carrierList = binaryServiceInfo.selectNodes("./carriers/carrier");
for (int j = 0; j < serviceList.size(); j++) {
Node serviceInfo = (Node) serviceList.get(j);
String serviceId = serviceInfo.selectSingleNode("./serviceId").getText();
String serviceName = serviceInfo.selectSingleNode("./serviceName").getText();
for (int k = 0; k < carrierList.size(); k++) {
Node accountInfo = (Node) carrierList.get(k);
String carrierId = accountInfo.selectSingleNode("./carrierId").getText();
String carrierName = accountInfo.selectSingleNode("./carrierName").getText();
String key = shortCode + "::" + serviceId + "::" + carrierId;
System.out.println("shortCode=" + shortCode + " serviceId=" + serviceId + " carrierId=" + carrierId);
System.out.println("key=" + key);
tempBinaryMap.put(key, shortCode);
}
}
}
binaryMap = tempBinaryMap;
} catch (Exception e) {
e.printStackTrace();
}
}
public void refresh() {
initialMapping();
}
}
当刷新xml文件时,总是刷新失败,即使把classes下面的xml文件删除, this.getClass().getClassLoader().getResourceAsStream()仍能读取到数据,并且数据和服务启动时读取到的数据完全一样,查看了源代码,发现this.getClass().getClassLoader().getResourceAsStream()首先从java虚拟机中查找文件,如果找不到,再按文件路径查找,找到后放到虚拟机中,也就是说第一次读取的时候已经把文件放到Java虚拟机中了,所以再读取的时候,直接从虚拟机中读取,所以,不能刷新成功。 修改代码 doc = reader.read(this.getClass().getClassLoader, 修改代码如下,
InputStream input = null;
String path = this.getClass().getClassLoader().getResource(File.separator).getPath();
input = new FileInputStream(new File(path + File.separator + "BinaryServiceMapping.xml"));
doc = reader.read(input);
此时报了一个错误,
java.io.FileNotFoundException: F:\program%20files\tomcat-5.5.23\webapps\Agaro_4_0_Problem\WEB-INF\classes\BinaryServiceMapping.xml (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at com.classLoader.resource.BinaryServiceManager.initialMapping(BinaryServiceManager.java:35)
at com.classLoader.resource.BinaryServiceManager.refresh(BinaryServiceManager.java:68)
at com.classLoader.resource.BinaryServiceServlet.doGet(BinaryServiceServlet.java:10)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
at java.lang.Thread.run(Thread.java:595)
发现这样的写法系统不认空格键,也就是说文件夹的命名不能有空格键,把有空格的文件名的空格去掉,则能成功刷新xml文件。