【问题标题】:ClassNotFoundException: ContextLoaderListener, when using Intellij with maven tomcat8 plugin and PostResourcesClassNotFoundException:ContextLoaderListener,当使用带有 maven tomcat8 插件和 PostResources 的 Intellij
【发布时间】:2020-04-20 22:57:29
【问题描述】:

我正在使用带有 maven tomcat8 插件的 Intellij。我在上下文文件中添加了一个 PostResource:

    <Resources className="org.apache.catalina.webresources.StandardRoot">
     <PostResources base="{my_post_resources_path}}"
      className="org.apache.catalina.webresources.JarResourceSet"
      internalPath="/post-resources"
      webAppMount="/WEB-INF/classes/post-resources"/>
    </Resources>

它正在被拾取,没问题。当我从命令行执行 mvn tomcat8:run 时,它工作得很好。但是当我从 Intellij 中运行它时,从 Maven 侧边栏中,通过右键单击 tomcat8 插件,然后是 tomcat8:run,我得到了这个错误:

    SEVERE: Error configuring application listener of class 
    org.springframework.web.context.ContextLoaderListener
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    SEVERE: Error configuring application listener of class 
    org.springframework.web.context.request.RequestContextListener
    java.lang.ClassNotFoundException: org.springframework.web.context.request.RequestContextListener

我意识到这两个类都在 spring-web 中找到,我在我的 pom 中有一个依赖项:

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>

我已经确认在我的项目的 target/war/WEB-INF/lib 目录中,spring-web jar 在那里。我已经看过很多关于通过手动将 jars 添加到部署中来使其与 Eclipse 一起使用的帖子。而且我已经看到您可以在 Intellij 中执行类似的操作,在 File -> Project structure -> Artifacts 中,但看不到在哪里添加依赖项,而且我认为这无论如何都行不通。有人在一篇文章中说要添加 spring-context-support 作为依赖项,我做到了。结果一样。

我已经多次关闭并响应 Intellij,运行并重新运行 mvn clean install ... 没有什么能让 tomcat 从 Intellij 中正常启动。有什么想法吗?

更新:我发现从命令行运行时也会发生这种情况,所以这不是 Intellij 的事情。这似乎与我使用 web.xml 而不是 Java 配置的事实有关。我的理解是这应该适用于 web.xml。

在我的 web.xml 中我定义了:

    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

如果我将它们注释掉,错误就会消失。但当然,应用程序不会启动,因为没有 ContextLoaderListener。 :(

【问题讨论】:

    标签: maven spring-mvc embedded-tomcat-8


    【解决方案1】:

    问题是tomcat8-maven-plugin的配置,我错过了warSourceDirectory。

        <configuration>
          <port>8080</port>
          <path>/myapp</path>
          <addContextWarDependencies>true</addContextWarDependencies>
          <contextFile>${project.build.outputDirectory}/myapp.xml</contextFile>
          <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
        </configuration>
    

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 2018-04-02
      • 2013-03-22
      • 1970-01-01
      • 2012-12-18
      • 2015-01-17
      • 2010-12-12
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多