简体   繁体   English

Spring 引导 - 可部署战争 - ClassNotFoundException

[英]Spring Boot - Deployable War - ClassNotFoundException

I have been attempting to create a spring boot deployable war so that it can be deployed to a tomcat server (I would like to note that it runs fine as an executable jar).我一直在尝试创建一个 spring 引导可部署战争,以便可以将其部署到 tomcat 服务器(我想指出它作为可执行 jar 运行良好)。 However, I have been running into the following exception which occurs on my Tomcat 9 server (I have one running within IntelliJ and another one running on a linux server, both throw the same exception):但是,我在 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

From my understanding, this lives in the spring-context jar, which is correctly being pulled into the build via maven.据我了解,这存在于 spring-context jar 中,它通过 maven 正确地被拉入构建中。 I created a super simple demo app with spring boot and I really can't see why I would be running into this issue.我用 spring 启动创建了一个超级简单的演示应用程序,我真的不明白为什么会遇到这个问题。

pom.xml 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>

And here is the main application file:这是主要的应用程序文件:

DemoApplication.java 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";
  }
}

I am at a total loss as to why I'm hitting this ClassNotFoundException.我完全不知道为什么会遇到这个 ClassNotFoundException。 Looking at the war file, I do see all the appropriate libraries in the WEB-INF/lib folder.查看 war 文件,我确实在 WEB-INF/lib 文件夹中看到了所有适当的库。 The two files I pasted above are literally all the project I'm attempting to run is.我上面粘贴的两个文件实际上是我试图运行的所有项目。

I have looked at this guide: Spring Boot Traditional Deployment我看过这个指南: Spring 引导传统部署

If I remove extends SpringBootServletInitializer , then the error goes away, but then I of course don't really have a functional spring boot war file.如果我删除extends SpringBootServletInitializer ,那么错误就会消失,但是我当然没有真正的功能 spring 引导战争文件。

did you import spring-boot-starter-web?你导入spring-boot-starter-web了吗? This class is located in context lib that is imported by spring-boot-starter-web此 class 位于由 spring-boot-starter-web 导入的上下文库中

Well, apparently I had a lib in my Tomcat lib folder that was built to include ALL dependent libraries... which happens to have a spring dependency... so it was pulling in an older version of spring, which was causing a conflict.好吧,显然我在我的 Tomcat lib 文件夹中有一个 lib,该文件夹被构建为包含所有依赖库......恰好有一个 spring 依赖......所以它正在拉入旧版本的 Z2A2D595E6ED9A0B24F02Z,F2B,这导致了冲突。1346D With the jar swapped out with one that doesn't include all dependencies, it loaded.随着 jar 被替换为不包含所有依赖项的一个,它加载了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM