【问题标题】:NoClassDefFoundError: NestedIOException when running tests with Spring Boot 3.0.0-M4 / Spring 6.0.0-M5NoClassDefFoundError:使用 Spring Boot 3.0.0-M4 / Spring 6.0.0-M5 运行测试时出现 NestedIOException
【发布时间】:2023-02-14 19:43:32
【问题描述】:

我有一个使用 Spring Boot 的 Java-17 项目,我正在尝试对其进行模块化。为了充分利用模块化,我将其移植到 Spring Boot 3 里程碑 4 和 Spring 6 里程碑 5。

该项目由我的 Maven 管理。我能够让项目编译。但是,它无法执行由 Spring Boot 生成的标准 contextLoads() 测试。具体报错信息为:

[main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Could not load default TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes available.
java.lang.NoClassDefFoundError: jakarta/servlet/ServletContext

使用不同的 TestExecutionListener 实现,即 TransactionalTestExecutionListener 和 SqlScriptsTestExecutionListener,此错误消息重复了两次,但 Spring 最终加载了许多 TestExecutionListeners 并尝试执行测试。但是,测试失败:

java.lang.NoClassDefFoundError: org/springframework/beans/factory/aot/BeanFactoryInitializationAotProcessor
    [...series of java.base frames...]
    at spring.context@6.0.0-M5/org.springframework.context.annotation.AnnotationConfigUtils.registerAnnotationConfigProcessors(AnnotationConfigUtils.java:165)
    at spring.context@6.0.0-M5/org.springframework.context.annotation.AnnotationConfigUtils.registerAnnotationConfigProcessors(AnnotationConfigUtils.java:138)
    at spring.context@6.0.0-M5/org.springframework.context.annotation.AnnotatedBeanDefinitionReader.<init>(AnnotatedBeanDefinitionReader.java:88)
    at spring.context@6.0.0-M5/org.springframework.context.annotation.AnnotatedBeanDefinitionReader.<init>(AnnotatedBeanDefinitionReader.java:71)
    at spring.context@6.0.0-M5/org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:69)
    at spring.boot@3.0.0-M4/org.springframework.boot.ApplicationContextFactory.lambda$static$0(ApplicationContextFactory.java:55)
    at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$1(SpringBootContextLoader.java:120)
    at spring.boot@3.0.0-M4/org.springframework.boot.SpringApplication.createApplicationContext(SpringApplication.java:566)
    at spring.boot@3.0.0-M4/org.springframework.boot.SpringApplication.run(SpringApplication.java:309)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:132)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:123)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
    at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$8(ClassBasedTestDescriptor.java:363)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$9(ClassBasedTestDescriptor.java:363)
    [...many more frames, mostly from junit and surefire...]
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)

这个问题可能是由我的 module-info.java 中的一些错误引起的。我使用的 Spring 模块是:

    requires spring.beans;
    requires spring.context;
    requires spring.boot.autoconfigure;
    requires spring.core;
    requires spring.boot;
    requires spring.oxm;
    requires spring.web;
    requires spring.ws.core;
    [...]
    opens [my module] to spring.core;
    exports [my module] to spring.beans, spring.context;

我错过了什么?该应用程序在 Spring 5.3 / Spring Boot 2.7 上编译并运行良好,但我需要将其模块化才能创建安装程序。

【问题讨论】:

  • 您是否检查了 spring 3.0.0-M1..M5 的发行说明...是否需要更改...这是主要的版本更改...我希望需要更改的内容...我会首先让应用程序运行使用 2.7.4...将其迁移到 3.0.0-M5,然后将其模块化...(问题是为什么要模块化?)...
  • 该应用程序与 Spring Boot 2.7.4 和 Spring Framework 5.3 完美配合,甚至模块化。但是我们需要为它创建一个安装程序,为了让 jlink 工作(据我所知),我需要一个模块化的应用程序,而 Spring Boot 2.7 / Spring 5.3 则不需要。还是我在这里错了?
  • 如果你喜欢使用 jlink,所有依赖项都必须是模块,这在目前是不太可能的。此外,输出显示你正在使用 spring boot 3.0.0-XX,它改变了应该首先修复的东西(见发行说明)。 .

标签: java spring-boot maven spring-framework-beans milestone


【解决方案1】:

问题似乎与org.springframework:spring-beans:5.3.25有关,它还没有包含所需的类。偶然地,我们在迁移到 Spring 3.0 时将此版本固定在我们的 build.gradle 中。这应该不再是必要的了。

因此,您可以尝试从 build.gradlepom.xml 中删除 spring-beans,具体取决于您使用的是 Gradle 还是 Maven。这应该已经足够了。如果没有,您可以尝试gradle dependenciesmvn dependency:tree(安装了相关插件)来找出旧版本是否/在哪里被拉入。或者,您可以在build.gradle 或@ 中明确包含最新版本987654329@。

【讨论】:

    猜你喜欢
    • 2017-07-15
    • 2016-02-16
    • 2016-12-22
    • 2021-10-08
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多