You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Jar启动报错:无法找到ServerProperties自动配置Bean

问题:Spring Cloud Gateway启动时缺少ServerProperties Bean

我有一个结构简单的Spring Boot应用,包含Spring Gateway配置yaml文件和带主函数的Application主类。

构建Jar包的命令:

./gradlew clean bootJar -x test

构建过程无异常。

build.gradle.kts内容如下:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.3.2"
    id("io.spring.dependency-management") version "1.1.6"
    kotlin("jvm") version "1.9.24"
    kotlin("plugin.spring") version "1.9.24"
    application
}

application {
    mainClass.set("com.example.reverseproxy.ReverseProxyApplicationKt")
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
    sourceCompatibility = JavaVersion.VERSION_17
}

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

repositories {
    mavenCentral()
}

extra["springCloudVersion"] = "2023.0.3"

dependencies {
    /* kotlin co-routines */
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

    /* kotlin reflection */
    implementation("org.jetbrains.kotlin:kotlin-reflect")

    /* spring web */
//    implementation("org.springframework.boot:spring-boot-starter-webflux")

    /* spring cloud gateway */
    implementation("org.springframework.cloud:spring-cloud-starter-gateway")
    implementation("org.springframework.boot:spring-boot-starter-actuator")

    /* test - dependencies */
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")

//    testImplementation("io.projectreactor:reactor-test")
//    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
//    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation(kotlin("stdlib-jdk8"))

}

dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += "-Xjsr305=strict"
        jvmTarget = "17"
    }
}


tasks.withType<Test> {
    enabled = false
    useJUnitPlatform()
}

tasks.withType<Jar> {

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    manifest {
        attributes["Main-Class"] = "com.example.reverseproxy.ReverseProxyApplicationKt"
    }

    archiveBaseName.set("ReverseProxy")
    archiveVersion.set("0.0.1-SNAPSHOT")

    from(sourceSets.main.get().output)

    // Include dependencies in the JAR file
    dependsOn(configurations.runtimeClasspath)
    from({
        configurations.runtimeClasspath.get()
            .filter { it.name.endsWith("jar") }
            .map { zipTree(it) }
    })

    // exclude specific signed files from being included in the final JAR
    exclude("META-INF/*.SF")
    exclude("META-INF/*.DSA")
    exclude("META-INF/*.RSA")
}

tasks.named("bootJar") {
    dependsOn("jar")
}
tasks.named("bootJar") {
    mustRunAfter("jar")
}
tasks.named("bootDistZip") {
    dependsOn("jar")
}
tasks.named("bootDistZip") {
    mustRunAfter("jar")
}
tasks.named("bootDistTar") {
    dependsOn("jar")
}
tasks.named("bootDistTar") {
    mustRunAfter("jar")
}
tasks.named("startScripts") {
    dependsOn("bootJar")
}
tasks.named("startScripts") {
    mustRunAfter("bootJar")
}

运行启动命令:

java -jar build/libs/ReverseProxy-0.0.1-SNAPSHOT.jar

持续报错:

19:35:51.003 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext -- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'httpClientSslConfigurer' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration$NettyConfiguration.class]: Unsatisfied dependency expressed through method 'httpClientSslConfigurer' parameter 0: No qualifying bean of type 'org.springframework.boot.autoconfigure.web.ServerProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
19:35:51.006 [main] ERROR org.springframework.boot.SpringApplication -- Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'httpClientSslConfigurer' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration$NettyConfiguration.class]: Unsatisfied dependency expressed through method 'httpClientSslConfigurer' parameter 0: No qualifying bean of type 'org.springframework.boot.autoconfigure.web.ServerProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

尝试过添加以下配置但无效:

// ensure that both compiled classes and resources are included in the JAR file
from(sourceSets.main.get().output)
sourceSets.main.configure {
    resources.srcDirs("src/main").includes.addAll(arrayOf("**/*.*"))
}

注释掉implementation("org.springframework.cloud:spring-cloud-starter-gateway")依赖后应用可正常启动,但该依赖是反向代理必需的。

请问问题出在哪里?


解决方案

问题根源在于自定义的Jar任务与Spring Boot原生的bootJar任务冲突,导致自动配置所需的Bean无法正确加载:

  1. bootJar本身已经负责构建可执行胖包,包含依赖打包、自动配置元数据处理等逻辑,自定义的Jar任务重复打包破坏了Spring Boot的Jar结构。
  2. 手动添加的bootJar依赖jar任务的配置完全多余,干扰了bootJar的正常执行流程。

修复步骤

  1. 删除自定义打包逻辑:移除tasks.withType<Jar>整个代码块,以及所有关于bootJar、bootDistZip、bootDistTar依赖jar任务的配置。
  2. 保留原生bootJar任务:Spring Boot的bootJar会自动使用application块中配置的主类,无需手动指定Main-Class。

修改后的build.gradle.kts需删除以下内容:

tasks.withType<Jar> {
    // 所有自定义打包逻辑
}

tasks.named("bootJar") {
    dependsOn("jar")
}
tasks.named("bootJar") {
    mustRunAfter("jar")
}
tasks.named("bootDistZip") {
    dependsOn("jar")
}
tasks.named("bootDistZip") {
    mustRunAfter("jar")
}
tasks.named("bootDistTar") {
    dependsOn("jar")
}
tasks.named("bootDistTar") {
    mustRunAfter("jar")
}
  1. 可选:显式添加WebFlux依赖(若仍有问题):Spring Cloud Gateway依赖WebFlux,虽然spring-cloud-starter-gateway已间接引入,但显式添加可避免版本冲突:
implementation("org.springframework.boot:spring-boot-starter-webflux")

重新构建启动

执行以下命令重新构建并启动应用:

./gradlew clean bootJar -x test
java -jar build/libs/ReverseProxy-0.0.1-SNAPSHOT.jar

内容的提问来源于stack exchange,提问作者Sachin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 03:00:54