java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener的解决办法

昨天在一个Maven+Spring4.1+SpringMvc4.1+Mybatis3的项目中启动Tomcat时提示如下错误

3 10, 2016 9:25:42 午前 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MyProject' did not find a matching property.
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server version:        Apache Tomcat/8.0.32
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server built:          Feb 2 2016 19:34:53 UTC
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server number:         8.0.32.0
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: OS Name:               Windows 7
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: OS Version:            6.1
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Architecture:          amd64
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Java Home:             C:\Program Files\Java\jdk1.8.0_60\jre
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: JVM Version:           1.8.0_60-b27
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: JVM Vendor:            Oracle Corporation
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: CATALINA_BASE:         E:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: CATALINA_HOME:         E:\Develope\apache-tomcat-8.0.32
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dcatalina.base=E:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dcatalina.home=E:\Develope\apache-tomcat-8.0.32
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dwtp.deploy=E:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Djava.endorsed.dirs=E:\Develope\apache-tomcat-8.0.32\endorsed
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dfile.encoding=MS932
3 10, 2016 9:25:42 午前 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
情報: Loaded APR based Apache Tomcat Native library 1.2.4 using APR version 1.5.1.
3 10, 2016 9:25:42 午前 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
情報: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
3 10, 2016 9:25:42 午前 org.apache.catalina.core.AprLifecycleListener initializeSSL
情報: OpenSSL successfully initialized (OpenSSL 1.0.2e 3 Dec 2015)
3 10, 2016 9:25:42 午前 org.apache.coyote.AbstractProtocol init
情報: Initializing ProtocolHandler ["http-apr-8080"]
3 10, 2016 9:25:42 午前 org.apache.coyote.AbstractProtocol init
情報: Initializing ProtocolHandler ["ajp-apr-8009"]
3 10, 2016 9:25:42 午前 org.apache.catalina.startup.Catalina load
情報: Initialization processed in 1109 ms
3 10, 2016 9:25:42 午前 org.apache.catalina.core.StandardService startInternal
情報: サービス Catalina を起動します
3 10, 2016 9:25:42 午前 org.apache.catalina.core.StandardEngine startInternal
情報: Starting Servlet Engine: Apache Tomcat/8.0.32
3 10, 2016 9:25:43 午前 org.apache.catalina.core.StandardContext listenerStart
重大: クラス org.springframework.web.context.ContextLoaderListener のアプリケーションリスナの設定中にエラーが発生しました
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
 at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
 at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
 at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4734)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at java.lang.Thread.run(Thread.java:745)

3 10, 2016 9:25:43 午前 org.apache.catalina.core.StandardContext listenerStart
重大: クラス org.springframework.web.context.request.RequestContextListener のアプリケーションリスナの設定中にエラーが発生しました
java.lang.ClassNotFoundException: org.springframework.web.context.request.RequestContextListener
 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
 at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
 at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
 at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4734)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at java.lang.Thread.run(Thread.java:745)

3 10, 2016 9:25:43 午前 org.apache.catalina.core.StandardContext listenerStart
重大: クラス org.springframework.web.util.IntrospectorCleanupListener のアプリケーションリスナの設定中にエラーが発生しました
java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
 at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
 at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
 at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
 at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4734)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at java.lang.Thread.run(Thread.java:745)

3 10, 2016 9:25:43 午前 org.apache.catalina.core.StandardContext listenerStart
重大: 前のエラーのためにアプリケーションリスナのインストールをスキップします
3 10, 2016 9:25:43 午前 org.apache.catalina.core.StandardContext startInternal
重大: One or more listeners failed to start. Full details will be found in the appropriate container log file
3 10, 2016 9:25:43 午前 org.apache.catalina.core.StandardContext startInternal
重大: 以前のエラーのためにコンテキストの起動が失敗しました [/MyProject]
3 10, 2016 9:25:43 午前 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler ["http-apr-8080"]
3 10, 2016 9:25:43 午前 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler ["ajp-apr-8009"]
3 10, 2016 9:25:43 午前 org.apache.catalina.startup.Catalina start
情報: Server startup in 733 ms


从异常信息来看应该属于没有找到相应的jar包,但是查看项目里确实都已经引入了这些jar包。不过这些jar包的位置是在下面这个位置

于是联想到eclipse加载jara包时是有一定顺序的。如果我们没有自己指定ClassLoader,那么eclipse就会默认的以.ClassPath中的顺序去加载项目需要的jar包。

于是到项目的根目录下以文本方式打开.ClassPath文件,发现里面果然没有上面的那个maven的路径。没有关系,既然没有那就手动的给它加上。修改完以后的.ClassPath如下

其中红色的部分是新追加的

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
 <classpathentry kind="src" output="target/classes" path="src/main/java">
  <attributes>
   <attribute name="optional" value="true"/>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
  <attributes>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry kind="src" output="target/test-classes" path="src/test/java">
  <attributes>
   <attribute name="optional" value="true"/>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
  <attributes>
   <attribute name="maven.pomderived" value="true"/>
  </attributes>
 </classpathentry>
 <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER">
     <attributes>
         <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
     </attributes>
 </classpathentry>
 <classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
  <attributes>
   <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
  </attributes>
 </classpathentry>
 <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
  <attributes>
   <attribute name="owner.project.facets" value="java"/>
  </attributes>
 </classpathentry>
 <classpathentry exported="true" kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
 <classpathentry exported="true" kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
 <classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
 <classpathentry kind="output" path="target/classes"/>
</classpath>

然后再打开项目根目录下.Project文件,发现里面也没有跟maven相关的路径,同样,也手动给它加上。

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
 <name>MyProject</name>
 <comment></comment>
 <projects>
 </projects>
 <buildSpec>
  <buildCommand>
   <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
   <arguments>
   </arguments>
  </buildCommand>
  <buildCommand>
   <name>org.eclipse.wst.common.project.facet.core.builder</name>
   <arguments>
   </arguments>
  </buildCommand>
  <buildCommand>
   <name>org.eclipse.jdt.core.javabuilder</name>
   <arguments>
   </arguments>
  </buildCommand>
  <buildCommand>
   <name>org.eclipse.m2e.core.maven2Builder</name>
   <arguments>
   </arguments>
  </buildCommand>
  <buildCommand>
   <name>org.eclipse.wst.validation.validationbuilder</name>
   <arguments>
   </arguments>
  </buildCommand>
  <buildCommand>
            <name>org.maven.ide.eclipse.maven2Builder</name>
            <arguments>
            </arguments>
  </buildCommand>

 </buildSpec>
 <natures>
  <nature>org.maven.ide.eclipse.maven2Nature</nature>
  <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
  <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
  <nature>org.eclipse.jdt.core.javanature</nature>
  <nature>org.eclipse.m2e.core.maven2Nature</nature>
  <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
  <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
 </natures>
</projectDescription>

保存以上修改以后,现在重启eclipse,然后在启动Tomcat,发现问题已经解决。

以上是自己在项目中遇到的一些小问题,希望能对学习Spring和Maven的程序猿们有些帮助。


阅读更多
文章标签: spring tomcat spring mvc
个人分类: Java学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭