您的位置:首页 > 编程语言 > Java开发

Spring-Struts2整合异常:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi

2015-12-02 12:34 811 查看
今天学习Spring和Struts2整合,其中Spring版本为spring-framework-4.1.5,在web.xml中配置了spring的上下文监听器,采用的是默认配置,具体如下:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<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”这个类,但是经过检查,该类位于:spring-web-4.1.5.RELEASE.jar包下,我确实已经导入了相关的jar包。
 
 
搞了两个小时,终于在网上知道原因所在:
Java虚拟机是根据JavaClassLoader(类加载器)决定如何加载Class。  

系统默认提供了3个ClassLoader:Root ClassLoader,ClassPath Loader,Ext ClassLoader  

我们也可以编写自己的ClassLoader,去加载特定环境下的Jar文件。    

能不能加载Jar,加载哪里的Jar,是由ClassLoader决定的。    

  

出该该问题可能是导入的仅仅是jar包的引用,例如Eeclipse中通过build path加进user library……(类似快捷方式)  

这种在Java Application中没问题,但在webApplication中可能会出现找不到类的异常。  

在WEB Application中jar包最好放在webroot或webcontent下的lib文件夹内,特别是xml中用到的jar包。 
 
我就是通过 User Library 快捷导入的jar包,没有把jar文件拷贝到lib目录下。
随后我将User Library 快捷导入的包删除,将所需的jar包全部拷贝到WEB-INF/lib下,然后导入,问题解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: