`
Josh_Persistence
  • 浏览: 1632274 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

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

/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下面

分享到:
评论

相关推荐

    mysql-connector-java.zip 两个版本:5.1.47、8.0.28

    java连接mysql数据库的驱动,里边包含两个版本:5.1.47、8.0.28 也可从以下两个地址免费下载: 1. https://repo1.maven.org/maven2/mysql/mysql-connector-java/ 2. ...

    Maven archetype-catalog.xml

    官网链接地址:http://repo1.maven.org/maven2/archetype-catalog.xml 百度云链接:https://pan.baidu.com/s/1dF8Qa9V 密码:37me

    spring-data-keyvalue-2.3.9.RELEASE-API文档-中文版.zip

    标签:springframework、data、spring、keyvalue、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

    spring-webmvc-5.0.8.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    apache-maven-3.3.9.rar

    2) A directory called "apache-maven-3.x.y" will be created. 3) Add the bin directory to your PATH, eg: Unix-based operating systems (Linux, Solaris and Mac OS X) export PATH=/usr/local/apache-...

    spring-boot-autoconfigure-2.2.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-boot-...标签:springframework、boot、spring、autoconfigure、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,

    spring-security-core-5.3.9.RELEASE-API文档-中文版.zip

    标签:springframework、security、spring、core、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

    spring-beans-5.0.10.RELEASE-API文档-中文版.zip

    标签:spring、beans、springframework、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    标签:springframework、spring、aop、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

    scala-java8-compat_2.11-0.7.0-API文档-中文版.zip

    Maven坐标:org.scala-lang.modules:scala-java8-compat_2.11:0.7.0; 标签:scala、lang、modules、java8、compat、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可...

    spring-beans-5.2.0.RELEASE-API文档-中英对照版.zip

    标签:springframework、spring、beans、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...

    spring-messaging-4.3.12.RELEASE-API文档-中英对照版.zip

    标签:springframework、spring、messaging、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

    spring-webmvc-5.2.15.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-plugin-metadata-2.0.0.RELEASE-API文档-中文版.zip

    标签:spring、metadata、plugin、springframework、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释...

    springfox-spring-webmvc-3.0.0-API文档-中英对照版.zip

    标签:spring、springfox、webmvc、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    maven资源 apache-maven-3.3.9-bin.zip

    maven资源 apache-maven-3.3.9-bin.zip

    spring-plugin-core-2.0.0.RELEASE-API文档-中文版.zip

    标签:spring、core、plugin、springframework、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

    spring-context-5.2.15.RELEASE-API文档-中文版.zip

    标签:springframework、spring、context、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-messaging-4.3.20.RELEASE-API文档-中文版.zip

    对应Maven信息:groupId:org.springframework,artifactId:spring-messaging,version:4.3.20.RELEASE 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中...

    spring-data-commons-2.0.6.RELEASE-API文档-中文版.zip

    标签:springframework、data、spring、commons、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

Global site tag (gtag.js) - Google Analytics