1

我使用 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 上的示例,但仍然出现错误......如何解决这个问题?

4

1 回答 1

0

与包含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.RELEASE. ConditionalOnJava确实包含JavaVersion版本中的子类1.15.14.RELEASEConditionalOnJava$JavaVersion错误消息的一部分)。但是,Gradle 也告诉我们spring-boot-actuator升级到2.5.1并且该版本不再包含子类JavaVersion

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

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

可以通过带有 starter的Micrometer将执行器指标发送到 Datadog ,有关详细信息io.micrometer:micrometer-registry-datadog,请参阅执行器指标文档。

于 2021-10-26T14:16:07.200 回答