java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL

今天学习spring+cxf的时候遇到一个问题:在web.xml中配置了spring的上下文监听器:

Xml代码  收藏代码
  1. <listener>  
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. </listener>  

 随后启动tomcat服务器,控制台提示如下错误:

Java代码  收藏代码
  1. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener  

这种错误意思很明确:找不到“ org.springframework.web.context.ContextLoaderListener”这个类,ContextLoaderListener这个类是在spring-web.jar包下,我仔细检查了项目jar环境,发现该jar包确实存在,而且也能找到编译后的ContextLoaderListener.class文件。

当时很疑惑,随后去网上找答案,终于发现问题根源:

Java代码  收藏代码
  1. Java虚拟机是根据Java ClassLoader(类加载器)决定如何加载Class。  
  2. 系统默认提供了3个ClassLoader    
  3. Root ClassLoader,ClassPath Loader,Ext ClassLoader  
  4. 我们也可以编写自己的ClassLoader,去加载特定环境下的Jar文件。    
  5. 能不能加载Jar,加载哪里的Jar,是由ClassLoader决定的。    
  6.   
  7. 楼主的问题可能是 导入的仅仅是jar包的引用,例如在eclipse中通过build path加进user lib……(类似快捷方式)  
  8. 这种在Java Application中没问题,但在web Application中可能会出现找不到类的异常。  
  9. 在WEB Application中jar包最好放在webroot或webcontent下的lib文件夹内,特别是xml中用到的jar包。  

因为我是通过eclipse的build path直接引用的jar包,没有把jar文件拷贝到lib目录下。

随后我将所需的jar包全部拷贝到WEB-INF/lib下,再重新启动tomcat便能顺利通过了。

原文地址:https://www.cnblogs.com/toSeeMyDream/p/4107475.html