Spring/Maven Exception - java.lang.ClassNotFoundException:ContextLoaderListener

Spring/Maven Exception - java.lang.ClassNotFoundException:ContextLoaderListener

 

在Spring构架的Web应用中,如果将该应用部署在Eclipse的Tomcat中,常常会出现java.lang.ClassNotFoundException:ContextLoaderListener Exception,该Exception主要有如下几个方面的原因。

1.  没有在web.xml中加载Spring的ContextLoaderListener(默认情况下,Spring会自动加载applicationContext.xml, 如果需要让Spring加载其它的配置文件,则需要在web.xml中配置ContextLoaderListener)

<context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/any.xml</param-value>

    </context-param>

 

<!-- In order to Spring load the configuration files except court-servlet.xml, 

you can add the files on listener:ContextLoaderListener. Spring will load

扫描二维码关注公众号,回复: 1148261 查看本文章

/WEB-INF/applicationContext.xml by default -->

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

 

2.  如果完成上诉配置后,还有同样的错误,则很有可能是spring-web.jar(spring2.5以后)或者是spring.jar(spring2.5以前)没有加载到tomcat的lib中,最简单的办法就是将该jar包直接放到web的src的lib目录下,及WEB-INF/lib目录下。然后重新部署。

 

3. 对于上面#2的解决方案,对于一般的web应用来说是个快捷的解决方案,但是如果该项目是Maven管理的,#2的方式显然就将Maven扼杀了。故可以使用Eclipse提供的集装箱式的部署方式将Maven的Dependency部署到web-inf/lib目录下。

1).右键点击项目--选择Properties

选择Deployment Assembly,在右边点击Add按钮,在弹出的窗口中选择Java Build Path Entries。如下图所示:

2).点击Next,选择Maven Dependencies

3).点击Finish,然后可以看到已经把Maven Dependencies添加到Web应用结构中了

操作完后,重新部署工程,不再报错了。然后我们再到.metadata\.plugins \org.eclipse.wst.server.core\tmp0\wtpwebapps\目录下,发现工程WEB-INF目录下自动生成了lib目 录,并且所有的依赖jar包也都已经部署进来。问题因此解决。

4. 对于今天Maven准备一统天下的项目管理模式下,#3的方案应是最简单的方案,这里再介绍方案4,方案4比较麻烦,而且容易出错,但是能然你真正了解到#3方案究竟做了什么东西,对于一个真正的发烧友来说,应该知道这个方案。

.1) 项目根目录下的.project文件,用记事本打开,加入以下代码(把原来的<buildSpec>节点和<natures>替换了):

复制代码
  <buildSpec>
        <buildCommand>
            <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.wst.common.project.facet.core.builder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.wst.validation.validationbuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</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.m2e.core.maven2Nature</nature>
        <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
        <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
        <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
    </natures>
复制代码


2).项目根目录下的.classpath,找到

<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>

替换为:

<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节点:

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
    </attributes>
</classpathentry>

OK,到这一步已经完成了,到eclipse中刷新项目,然后重新启动tomcat,错误已经解决!

在.classpath文件中:

<classpathentry kind="output" path="target/classes"/>

改为:

<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>

注: J2EE良好的编程习惯让我们常常把编译后的文件放到WEB-INF/classes下面

猜你喜欢

转载自josh-persistence.iteye.com/blog/1874851