今天學習spring+cxf的時候遇到一個問題:在web.xml中配置了spring的上下文監聽器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
隨後啓動tomcat服務器,控制檯提示如下錯誤:
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
這種錯誤意思很明確:找不到“ org.springframework.web.context.ContextLoaderListener”這個類,
ContextLoaderListener這個類是在spring-web.jar包下,我仔細檢查了項目jar環境,發現該jar包確實存在,而且也能找到編譯後的ContextLoaderListener.class文件。
當時很疑惑,隨後去網上找答案,終於發現問題根源:
Java虛擬機是根據Java ClassLoader(類加載器)決定如何加載Class。
系統默認提供了3個ClassLoaderRoot ClassLoader,ClassPath Loader,Ext ClassLoader
我們也可以編寫自己的ClassLoader,去加載特定環境下的Jar文件。
能不能加載Jar,加載哪裏的Jar,是由ClassLoader決定的。
樓主的問題可能是 導入的僅僅是jar包的引用,例如在eclipse中通過build path加進user lib……(類似快捷方式)
這種在Java Application中沒問題,但在web Application中可能會出現找不到類的異常。
在WEB Application中jar包最好放在webroot或webcontent下的lib文件夾內,特別是xml中用到的jar包。
因爲我是通過eclipse的build path直接引用的jar包,沒有把jar文件拷貝到lib目錄下。
隨後我將所需的jar包全部拷貝到WEB-INF/lib下,再重新啓動tomcat便能順利通過了。