当前位置: 首页 > 知识库问答 >
问题:

使用Spring Boot“bootrun”启动的应用程序在包含Gradle Vaadin插件时导致NoClassDefFoundError

文喜
2023-03-14

我的目标是建立一个简单的多模块项目,它使用Spring Boot2、Gradle4.x和Vaadin8.x作为UI。Vaadin目前只用于其中的一个子项目,其他的子项目提供服务或只是库。

我从这个非常好的Spring教程开始,它(尽管使用了旧的Gradle语法)产生了一个可以工作的测试项目。它包含一个“Libaray”子项目和一个依赖于前者的“Application”子项目。“bootrun”和“bootjar”Gradle任务都像一个魅力一样工作,并且在我将一些配置集中在父构建中后仍然工作。grade。

然后我将Vaadin Gradle插件添加到“Application”项目中,并更改endpoint以显示一个简单的Vaadin标签。

问题是,现在使用“bootrun”时,启动的应用程序不再能够访问它所依赖的“库”中的类。如果我删除代码中的任何依赖项,应用程序就可以工作,但一旦我从“Library”(例如“MyService”)中引用了一个类,它就会以“java.lang.ClassNotFoundException”崩溃。

但是,当使用“bootjar”部署相同的应用程序并运行jar时,一切都可以工作,因此模块间依赖关系可以得到解决。

以下是相关的示例代码(不包括导入):

Hello.app.DemoApplication

@SpringBootApplication(scanBasePackages = "hello")
@RestController
public class DemoApplication {
    //Service defined in dependent sub-project
    private final MyService myService;

    public DemoApplication(MyService myService) {
        this.myService = myService;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

hello.app.startup

/** Entry point for the Vaadin 8 UI */
@SpringUI
@SpringView
public class StartUI extends UI implements View {

    @Override
    protected void init(final VaadinRequest request) {
        setContent(new Label("Hello vaadin"));
    }
}
plugins { id "io.spring.dependency-management" version "1.0.5.RELEASE" }
ext { springBootVersion = '2.0.3.RELEASE' }
jar {
    baseName = 'library'
    version = '0.0.1-SNAPSHOT'
}
dependencies {
    implementation('org.springframework.boot:spring-boot-starter')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
    imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") }
}
plugins {
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
    id "org.springframework.boot" version "2.0.3.RELEASE"
    id 'com.devsoap.plugin.vaadin'version '1.3.1' //Add vaadin support
}
bootJar {
    baseName = 'application'
    version = '0.0.1-SNAPSHOT'
}
dependencies {
    implementation project(':library') // Contains "MyService"
    implementation('org.springframework.boot:spring-boot-starter-actuator')
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}
subprojects {
    //Only plugins configured here applied to sub-projects.
    apply plugin: "java-library"
    apply plugin: "eclipse"
    repositories { mavenCentral() } 
    sourceCompatibility = 1.8
}
java.lang.IllegalStateException: Cannot load configuration class: hello.app.DemoApplication
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:414) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:254) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:128) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    ...
Caused by: java.lang.IllegalStateException: Unable to load cache item
    at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:138) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:110) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:403) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 12 common frames omitted
Caused by: java.lang.NoClassDefFoundError: hello/service/MyService
    at java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:1.8.0_151]
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) ~[na:1.8.0_151]
    at java.lang.Class.getDeclaredConstructors(Class.java:2020) ~[na:1.8.0_151]
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:566) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ...
Caused by: java.lang.ClassNotFoundException: hello.service.MyService
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_151]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_151]
    ... 34 common frames omitted

共有1个答案

马坚
2023-03-14

Spring依赖项管理插件真的很麻烦,并不是所有的插件都支持它(比如Gradle Vaadin插件)。我会跳过它,而是使用Gradle的BOM支持。https://docs.gradle.org/4.6/userguide/managing_transitive_dependencies.html#sec:bom_import

 类似资料:
  • 使用spring-boot时,一切工作都很好。尽管如此,在spring-boot中已删除了注释和。我试图将代码重构为新版本,但我做不到。对于以下测试,我的应用程序在测试之前没有启动,http://localhost:8080返回404: 如何重构测试以使其在Spring-Boot1.5中工作?

  • 我使用 启动了一个 Spring boot 应用程序。 在我启动命令的终端中执行ctrl-c不会停止应用程序。 那么,阻止它的正确方法是什么呢?

  • 我有一份Java申请。 应用程序有一个决定应用程序是否在启动时启动的设置。 目前,我通过在StartUp items文件夹中放置/删除快捷方式实现了这一点。 然而,我想知道是否有更好的方法来处理这种行为。 编辑 是的,是视窗。抱歉之前没有清除。 应用程序有一个UI,用户可以在其中触发操作,并且应用程序在运行时定期在后台运行一些任务。 @Peter,如何使用应用程序中的代码更改注册表?这种方法是否与

  • 我在stackoverflow上发现了类似的问题,并试图用这种方式(LINK)解决这个问题,但在我的项目中没有起作用。谁能给我一些建议吗? pom.xml 应用属性

  • 我想启用或禁用具有外部配置的SSL/TLS,这些配置可以在应用程序启动期间提供。应用程序应该支持http和HTTPS的所有crud操作。 既然上面的属性是不推荐使用的,那么我如何在不使用配置文件的情况下实现它。

  • 最近突然出现了一个问题,只要在pubspec.yaml中包含“location”中的依赖项就可以了。 不幸的是,看颤振日志没有线索...