微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

STS 4.9.0 中 OSGI 资源的类路径问题

如何解决STS 4.9.0 中 OSGI 资源的类路径问题

我的一个 Spring Boot 应用程序无法在我的 IDE (STS 4.9.0) 中运行。 Maven Build 运行没有任何问题。但是,如果我想使用启动仪表板在 STS 中运行该应用程序,则会出现以下错误

    java.lang.NoSuchMethodError: org.springframework.core.type.AnnotationMetadata.introspect(Ljava/lang/Class;)Lorg/springframework/core/type/AnnotationMetadata;
        at org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:108)
        at org.springframework.context.annotation.ConfigurationClasspostProcessor.processConfigBeanDeFinitions(ConfigurationClasspostProcessor.java:276)
        at org.springframework.context.annotation.ConfigurationClasspostProcessor.postProcessBeanDeFinitionRegistry(ConfigurationClasspostProcessor.java:236)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDeFinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokebeanfactoryPostProcessors(PostProcessorRegistrationDelegate.java:96)
        at org.springframework.context.support.AbstractApplicationContext.invokebeanfactoryPostProcessors(AbstractApplicationContext.java:707)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
        at de.berlin.itdz.forms4itdz.kita.app.KitaApp.main(KitaApp.java:27)
    05.01.2021 08:35:44 ERROR [main] 
    
    ***************************
    APPLICATION Failed TO START
    ***************************
    
    Description:
    
    An attempt was made to call a method that does not exist. The attempt was made from the following location:
    
        org.springframework.context.annotation.ConfigurationClassUtils.checkConfigurationClassCandidate(ConfigurationClassUtils.java:108)
    
    The following method did not exist:
    
        org.springframework.core.type.AnnotationMetadata.introspect(Ljava/lang/Class;)Lorg/springframework/core/type/AnnotationMetadata;
    
    The method's class,org.springframework.core.type.AnnotationMetadata,is available from the following locations:
    
        jar:file:/Z:/admpetersen/sts-4.9.0.RELEASE/configuration/org.eclipse.osgi/99/0/.cp/lib/spring/spring-core-5.1.4.RELEASE.jar!/org/springframework/core/type/AnnotationMetadata.class
        jar:file:/Z:/admpetersen/.m2/repository/org/springframework/spring-core/5.2.10.RELEASE/spring-core-5.2.10.RELEASE.jar!/org/springframework/core/type/AnnotationMetadata.class
    
    The class hierarchy was loaded from the following locations:
    
        org.springframework.core.type.AnnotationMetadata: file:/Z:/admpetersen/sts-4.9.0.RELEASE/configuration/org.eclipse.osgi/99/0/.cp/lib/spring/spring-core-5.1.4.RELEASE.jar
    
    
    Action:
    
    Correct the classpath of your application so that it contains a single,compatible version of org.springframework.core.type.AnnotationMetadata
    
    05.01.2021 08:35:44 DEBUG [main] Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@388ffbc2,started on Tue Jan 05 08:35:44 CET 2021

消息告诉我,加载了两个不同版本的 spring 核心库,一个是 maven 依赖项的正确版本,另一个是包含的 osgi-bundle 中的错误版本。我真的很想更正消息中建议的类路径以排除 Osgi 资源。但我不知道怎么做。在项目属性(Java 构建路径)和 Spring Boot 应用程序的调试配置中,都没有提到 Osgi 库的使用。显然,它们是隐含的。有人知道如何让他们远离类路径吗?提前致谢!

一些附加说明:

  • 启用了以下项目方面:Dynamic Web Module 4.0、Java 1.8、JavaScript 1.0、JavaServer Faces 2.2
  • .project 的内容
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>kita-formular-boot</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.wst.common.project.facet.core.builder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.wst.validation.validationbuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
        <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
        <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
        <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
        <nature>com.jaspersoft.studio.babel.editor.rbeNature</nature>
    </natures>
</projectDescription>

解决方法

现在我知道了原因,这听起来很简单,但我确实花了一些时间才弄明白。导致这种奇怪行为的是一系列依赖关系:

  1. 在我的 STS 中,我安装了 Jaspersoft Studio。
  2. 在 kita-formular-boot 项目的 Java 构建路径中,我添加了另一个项目 (kita-resources) 作为必需项目。
  3. kita-resources 包含一些 jrxml 文件。为了编译它们,我在项目中添加了 JasperReports Nature。
  4. 这样做时,JasperReports 库依赖项会自动添加到 Java 构建路径中。
  5. 这些依赖项包括导致问题的旧 spring-core 版本。

通过将 kita-resources 设置为必需项目,kita-resources 中的所有类路径也包含在 kita-formular-boot 中。所以我所要做的就是从 kita-resources 的 Java 构建路径中删除 JasperReports 库依赖项。显然,它们不是编译项目所必需的。所有的冲突都消失了!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。