【问题标题】:Spring Boot - Deployable War - ClassNotFoundExceptionSpring Boot - 可部署战争 - ClassNotFoundException
【发布时间】:2021-04-04 02:30:32
【问题描述】:

我一直在尝试创建一个 spring boot 可部署战争,以便可以将其部署到 tomcat 服务器(我想指出它作为可执行 jar 运行良好)。但是,我在 Tomcat 9 服务器上遇到了以下异常(我有一个在 IntelliJ 中运行,另一个在 linux 服务器上运行,两者都抛出相同的异常):

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.demo.DemoApplication]; nested exception is java.lang.NoClassDefFoundError: org/springframework/context/annotation/AdviceMode

据我了解,它存在于 spring-context jar 中,该 jar 已通过 maven 正确地拉入构建中。我用 spring boot 创建了一个超级简单的演示应用程序,我真的不明白为什么会遇到这个问题。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.4</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>11</java.version>
    <start-class>com.example.demo.DemoApplication</start-class>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.5</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

这是主要的应用程序文件:

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication  extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DemoApplication.class);
  }
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @RequestMapping(value = "/")
  public String hello() {
    return "Hello World from Tomcat";
  }
}

我完全不知道为什么会遇到这个 ClassNotFoundException。查看 war 文件,我确实在 WEB-INF/lib 文件夹中看到了所有适当的库。我上面粘贴的两个文件实际上就是我试图运行的所有项目。

我看过这个指南:Spring Boot Traditional Deployment

如果我删除 extends SpringBootServletInitializer,那么错误就会消失,但是我当然没有真正的功能性 Spring Boot War 文件。

【问题讨论】:

    标签: java spring-boot spring-mvc


    【解决方案1】:

    您是否导入了 spring-boot-starter-web?该类位于 spring-boot-starter-web 导入的上下文库中

    【讨论】:

    • 是的,我已经导入了它。我的问题最终成为我的 Tomcat lib 文件夹中的一个坏库,由于某种原因捆绑了旧版本的 spring。我用一个不捆绑所有依赖项的库替换了库,我的问题就消失了。
    【解决方案2】:

    好吧,显然我在我的 Tomcat lib 文件夹中有一个 lib,它被构建为包含所有依赖的库......它恰好有一个 spring 依赖......所以它正在拉入一个旧版本的 spring,这导致冲突。将 jar 替换为不包含所有依赖项的 jar,然后加载。

    【讨论】:

      猜你喜欢
      • 2019-03-23
      • 2018-07-05
      • 2019-03-21
      • 2015-10-28
      • 2017-09-21
      • 2018-04-12
      • 2016-08-16
      • 1970-01-01
      • 2018-08-11
      相关资源
      最近更新 更多