找不到类 [org.springframework.boot.autoconfigure.condition.ConditionalOnJava$JavaVersion]

Posted

技术标签:

【中文标题】找不到类 [org.springframework.boot.autoconfigure.condition.ConditionalOnJava$JavaVersion]【英文标题】:Could not find class [org.springframework.boot.autoconfigure.condition.ConditionalOnJava$JavaVersion] 【发布时间】:2021-12-11 13:50:55 【问题描述】:

我使用 intellij 和 java 1.8 来运行我的 springboot

我试图在我的代码上创建这样的主 jar:

@SpringBootApplication
@RestController
public class DemoApplication 

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

    private DemoMetricReaderWriter demoMetricReaderWriter = new DemoMetricReaderWriter();

    @Bean
    @ExportMetricReader
    @ExportMetricWriter
    DemoMetricReaderWriter getReader() 
        return demoMetricReaderWriter;
    

    @RequestMapping("/")
    String home() throws Exception 
        long start = System.currentTimeMillis();

        // insert up to 2 second delay for a wider range of response times
        Thread.sleep((long) (Math.random() * 2000));

        // let that delay become the gauge.bar metric value
        long barValue = System.currentTimeMillis() - start;

        demoMetricReaderWriter.updateMetrics(barValue);
        return "Hello World!";
    

但是当我构建并运行我的 jar 时,我得到了这样的错误。我已经尝试修复,但我不知道这到底是什么问题

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.demo.DemoApplication]; nested exception is java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.condition.ConditionalOnJava$JavaVersion]

这是我用于创建此应用的 gradle.build。

    plugins 
    id 'org.springframework.boot' version '2.5.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'

jar 
    manifest 
        attributes 'Main-Class': 'com.example.demo.DemoApplication'
    


group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations 
    all 
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: 'ch.qos.logback', module: 'logback-classic'
    
    developmentOnly
    runtimeClasspath 
        extendsFrom developmentOnly
    
    compileOnly 
        extendsFrom annotationProcessor
    


repositories 
    mavenCentral()
    google()


dependencies 
    // spring boot
    implementation ('org.springframework.boot:spring-boot-starter-web') 
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

    // health check
    implementation 'org.springframework.boot:spring-boot-actuator:1.5.14.RELEASE'

    // logging
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    implementation 'com.lmax:disruptor:3.4.2'

    // database
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb:2.5.5'
    runtimeOnly 'com.oracle.ojdbc:ojdbc8'
    runtimeOnly 'org.postgresql:postgresql'


    //datadog
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'org.apache.httpcomponents:httpclient:4.5.3'
    implementation 'org.springframework.boot:spring-boot-actuator:1.5.14.RELEASE'


    // lombok
    compileOnly 'org.projectlombok:lombok'



    // testing
    testImplementation 'org.mockito:mockito-core:3.9.0'
    testImplementation('org.springframework.boot:spring-boot-starter-test') 
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'io.projectreactor:reactor-test'
    annotationProcessor 'org.projectlombok:lombok'


test 
    useJUnitPlatform()

我的问题是我的代码有什么问题?为了运行此代码,我使用 gradle 我尝试过使用 Internet 上的示例,但仍然出现错误......如何解决这个问题?

【问题讨论】:

【参考方案1】:

与包含ConditionalOnJava 的模块存在依赖冲突。 ConditionalOnJava 是模块 spring-boot-autoconfigure 的一部分。让我们看看 Gradle 选择了哪个版本。为此,我们发出以下命令:

gradlew -q dependencyInsight --dependency spring-boot-autoconfigure --configuration runtimeClasspath

对我们来说相关的输出是

org.springframework.boot:spring-boot-autoconfigure:1.5.14.RELEASE -> 2.5.1
\--- org.springframework.boot:spring-boot-actuator:1.5.14.RELEASE
     \--- runtimeClasspath

这告诉我们spring-boot-actuator:1.5.14.RELEASE 依赖于spring-boot-autoconfigure:1.5.14.RELEASEConditionalOnJava 确实在版本1.15.14.RELEASE 中包含一个子类JavaVersionConditionalOnJava$JavaVersion 错误消息的一部分)。但是,Gradle 也告诉我们 spring-boot-actuator 升级到 2.5.1 并且该版本不再包含子类 JavaVersion

恐怕您需要将 Spring Boot Actuator 升级到与您正在运行的 Spring Boot 版本相关的版本:

implementation 'org.springframework.boot:spring-boot-starter-actuator'

可通过Micrometer 使用启动器io.micrometer:micrometer-registry-datadog 向Datadog 发送执行器指标,有关详细信息,请参阅Actuator Metrics 文档。

【讨论】:

是的...我需要使用最新的执行器..但是如果我使用最新的执行器没有包 ExportMetricReader ExportMetricWriter 这个注释...我的其他问题是如果我想发送http状态登录到 datadog 怎么做? Datadog 集成现在可通过Micrometer 与启动器io.micrometer:micrometer-registry-datadog 进行,详情请参阅Actuator Metrics。

以上是关于找不到类 [org.springframework.boot.autoconfigure.condition.ConditionalOnJava$JavaVersion]的主要内容,如果未能解决你的问题,请参考以下文章

找不到类 [org.springframework.orm.hibernate.LocalSessionFactoryBean]

找不到类 [org.springframework.boot.autoconfigure.condition.ConditionalOnJava$JavaVersion]

找不到类 org.springframework.web.servlet.mvc.annotation.RequestMappingHandlerMapping

org.bson.codecs.configuration.CodecConfigurationException:找不到类 org.springframework.data.mongodb.core

Tomcat & Spring Web - 找不到类异常 org.springframework.web.context.ContextLoaderListener

idea启动tomcat 找不到 类,或者报Error configuring application listener of class org.springframework.web.contex