如何解决 SpringBoot 错误:从 Java11 迁移到 Java 23 后出现“组件需要一个名为‘entityManagerFactory’的 bean,但无法找到”

问题描述 投票:0回答:1

我正在致力于将一个大型现有项目从 Java 11/SpringBoot 2.4.11 迁移到 Java 21/SpringBoot 3.3.4。 我已遵循迁移指南并修复了已弃用的方法错误,现在运行

gradle clean build
可以成功执行。

但是,当我尝试启动应用程序时,我看到以下异常

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

16:24:24.624 [main] ERROR com.xxxx.xxx.xxx.Application main application exited
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaSharedEM_entityManagerFactory': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument

我已经研究过这个问题,这似乎是由不兼容的 Springboot 依赖项引起的,但我无法确定哪些依赖项是问题所在,因为我所有的 spring 依赖项似乎都匹配 3.3.4。

我的build.gradle文件:

buildscript {
    buildDir = 'target'
    ext {
        springBootVersion = '3.3.4'
        springVersion = '6.1.0'
    }
    repositories {
        maven {
            url 'https://artifactory.aexp.com/iq-maven-central-proxy'
        }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:3.3.4"
    }
}
plugins {
    id 'maven-publish'
    id 'java'
    id 'idea'
    id 'org.springframework.boot' version "${springBootVersion}"
    id 'distribution'
    id 'groovy'
    id 'jacoco'
    id 'io.spring.dependency-management' version '1.1.6' // Replace with the latest version

    //   id 'com.github.sherter.google-java-format' version '0.9'
//export JAVA_HOME=/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
}
//-Dorg.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

repositories {
    mavenLocal()
    maven {
        url 'https://artifactory.aexp.com/prod/'
    }
}
version = '1.0-SNAPSHOT'
description = 'submissionsweb'
//rootProject.name = 'submissionsweb'
configurations.all {
    exclude group: "log4j", module: "log4j"
    exclude group: "ch.qos.logback", module: "logback-classic"
    exclude group: "ch.qos.logback", module: "logback-core"
    exclude group: "org.apache.logging.log4j", module: "log4j-to-slf4j"
    exclude group: "org.apache.logging.log4j", module: "log4j-slf4j-impl"
}
dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-starter-jdbc")
    implementation("org.springframework.boot:spring-boot-starter-aop")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework:spring-webmvc:6.1.13")
    implementation("org.jboss.logging:jboss-logging:3.4.2.Final")

    annotationProcessor 'org.projectlombok:lombok:1.18.30'
    implementation 'org.projectlombok:lombok:1.18.30'
    implementation 'com.oracle.ojdbc:ojdbc8:19.3.0.0'
    implementation ("org.glassfish.jaxb:jaxb-runtime:2.3.2")
            {
                exclude (group: "jakarta.activation", module: "jakarta.activation-api")
                exclude (group: "jakarta.xml.bind", module: "jakarta.xml.bind-api")
            }
    implementation 'com.jcraft:jsch:0.1.55'
    implementation 'pl.pojo:pojo-tester:0.7.6'
    implementation 'com.google.guava:guava:32.0.1-jre'
    implementation('org.everit.json:org.everit.json.schema:1.5.1')
            {
                exclude (group: "commons-collections", module: "commons-collections")
                exclude (group: "commons-logging", module: "commons-logging")
            }
    implementation 'commons-io:commons-io:2.11.0'
    implementation 'org.slf4j:slf4j-api:2.0.0-alpha5'
    implementation 'org.slf4j:slf4j-simple:2.0.0-alpha5'
    implementation 'org.aspectj:aspectjtools:1.9.7'
    implementation 'com.ibm.db2:jcc:11.5.6.0'

    implementation ("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0"){
        exclude (group: "com.fasterxml.jackson.core", module: "jackson-annotations")
//        exclude (group: "com.fasterxml.jackson.dataformat", module: "jackson-dataformat-yaml")
        exclude (group: "com.fasterxml.jackson.datatype", module: "jackson-datatype-jsr310")
        exclude (group: "com.fasterxml.jackson.core", module: "jackson-databind")
        exclude (group: "jakarta.xml.bind", module: "jakarta.xml.bind-api")
        exclude(group: "org.springframework", module: "spring-beans")
        exclude(group: "org.springframework", module: "spring-webmvc")
    }

    implementation('io.springfox:springfox-bean-validators:2.9.2')
            {
                exclude (group: "com.fasterxml", module: "classmate")
                exclude(group: "net.bytebuddy", module: "byte-buddy")
                exclude(group: "org.springframework", module: "spring-aop")
                exclude(group: "org.springframework", module: "spring-beans")
                exclude(group: "org.springframework", module: "spring-context")
            }
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group: "net.bytebuddy", module: "byte-buddy")
        exclude group: "org.mockito", module: "mockito-junit-jupiter"
        exclude group: "org.mockito", module: "mockito-core"
        exclude (group: "net.minidev", module: "json-smart")
        exclude(group: "org.springframework.boot", module: "spring-boot-autoconfigure")
    }
    implementation("org.mockito:mockito-core:3.7.0")
            {
                exclude(group: "net.bytebuddy", module: "byte-buddy")
            }
    testImplementation("org.mockito:mockito-junit-jupiter:3.7.0")
            {
                exclude (group: "org.junit.jupiter", module: "junit-jupiter-api")
            }
//    testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
}
springBoot {
    mainClass = 'com.xxx.xxx.Application'
}
test {
    useJUnitPlatform()
    finalizedBy jacocoTestReport
    jacoco {
        destinationFile = file("$buildDir/jacoco.exec")
    }
}
jacocoTestReport {
    dependsOn test
    reports {
        xml.required = true
        csv.required = false
        xml.outputLocation = file("$buildDir/reports/jacoco/jacoco.xml")
        html.outputLocation = file("$buildDir/reports/jacoco/jacocoHtml")
    }
}

distributions {
    main {
        // baseName = 'app'
        contents {
            exclude("*SNAPSHOT*.jar")
            into('.') {
                from jar
                from "$buildDir/appBundle"
            }
            //.gradle
            from('$buildDir/appBundle') {
            }
        }
    }
}
distZip {
    doLast {
        archivesBaseName = 'app'
        archiveClassifier = 'bin'
    }
}
distributions {
    custom {
        distTar {
            archivesBaseName = 'app'
            archiveVersion.set("")
            archiveExtension.set("tar.gz")
            archiveClassifier.set("bin")
            compression = Compression.GZIP
        }

    }
}

./gradlew dependencies
命令的输出

+--- org.springframework.boot:spring-boot-starter-web -> 3.3.4
|    +--- org.springframework.boot:spring-boot-starter:3.3.4
|    |    +--- org.springframework.boot:spring-boot:3.3.4
|    |    |    +--- org.springframework:spring-core:6.1.13
|    |    |    |    \--- org.springframework:spring-jcl:6.1.13
|    |    |    \--- org.springframework:spring-context:6.1.13
|    |    |         +--- org.springframework:spring-aop:6.1.13
|    |    |         |    +--- org.springframework:spring-beans:6.1.13
|    |    |         |    |    \--- org.springframework:spring-core:6.1.13 (*)
|    |    |         |    \--- org.springframework:spring-core:6.1.13 (*)
|    |    |         +--- org.springframework:spring-beans:6.1.13 (*)
|    |    |         +--- org.springframework:spring-core:6.1.13 (*)
|    |    |         +--- org.springframework:spring-expression:6.1.13
|    |    |         |    \--- org.springframework:spring-core:6.1.13 (*)
|    |    |         \--- io.micrometer:micrometer-observation:1.12.10 -> 1.13.4
|    |    |              \--- io.micrometer:micrometer-commons:1.13.4
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:3.3.4
|    |    |    \--- org.springframework.boot:spring-boot:3.3.4 (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:3.3.4
|    |    |    \--- org.slf4j:jul-to-slf4j:2.0.16
|    |    |         \--- org.slf4j:slf4j-api:2.0.16 -> 2.0.0-alpha5
|    |    +--- jakarta.annotation:jakarta.annotation-api:2.1.1
|    |    +--- org.springframework:spring-core:6.1.13 (*)
|    |    \--- org.yaml:snakeyaml:2.2
|    +--- org.springframework.boot:spring-boot-starter-json:3.3.4
|    |    +--- org.springframework.boot:spring-boot-starter:3.3.4 (*)
|    |    +--- org.springframework:spring-web:6.1.13
|    |    |    +--- org.springframework:spring-beans:6.1.13 (*)
|    |    |    +--- org.springframework:spring-core:6.1.13 (*)
|    |    |    \--- io.micrometer:micrometer-observation:1.12.10 -> 1.13.4 (*)
|    |    +--- com.fasterxml.jackson.core:jackson-databind:2.17.2
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.17.2
|    |    |    |    \--- com.fasterxml.jackson:jackson-bom:2.17.2
|    |    |    |         +--- com.fasterxml.jackson.core:jackson-annotations:2.17.2 (c)
|    |    |    |         +--- com.fasterxml.jackson.core:jackson-core:2.17.2 (c)
|    |    |    |         +--- com.fasterxml.jackson.core:jackson-databind:2.17.2 (c)
|    |    |    |         +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.17.2 (c)
|    |    |    |         +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.2 (c)
|    |    |    |         \--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.17.2 (c)
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.17.2
|    |    |    |    \--- com.fasterxml.jackson:jackson-bom:2.17.2 (*)
|    |    |    \--- com.fasterxml.jackson:jackson-bom:2.17.2 (*)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.17.2
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.17.2 (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.17.2 (*)
|    |    |    \--- com.fasterxml.jackson:jackson-bom:2.17.2 (*)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.2
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.17.2 (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.17.2 (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.17.2 (*)
|    |    |    \--- com.fasterxml.jackson:jackson-bom:2.17.2 (*)
|    |    \--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.17.2
|    |         +--- com.fasterxml.jackson.core:jackson-core:2.17.2 (*)
|    |         +--- com.fasterxml.jackson.core:jackson-databind:2.17.2 (*)
|    |         \--- com.fasterxml.jackson:jackson-bom:2.17.2 (*)
|    +--- org.springframework.boot:spring-boot-starter-tomcat:3.3.4
|    |    +--- jakarta.annotation:jakarta.annotation-api:2.1.1
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:10.1.30
|    |    +--- org.apache.tomcat.embed:tomcat-embed-el:10.1.30
|    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:10.1.30
|    |         \--- org.apache.tomcat.embed:tomcat-embed-core:10.1.30
|    +--- org.springframework:spring-web:6.1.13 (*)
|    \--- org.springframework:spring-webmvc:6.1.13
|         +--- org.springframework:spring-aop:6.1.13 (*)
|         +--- org.springframework:spring-beans:6.1.13 (*)
|         +--- org.springframework:spring-context:6.1.13 (*)
|         +--- org.springframework:spring-core:6.1.13 (*)
|         +--- org.springframework:spring-expression:6.1.13 (*)
|         \--- org.springframework:spring-web:6.1.13 (*)
+--- org.springframework.boot:spring-boot-starter-data-jpa -> 3.3.4
|    +--- org.springframework.boot:spring-boot-starter-aop:3.3.4
|    |    +--- org.springframework.boot:spring-boot-starter:3.3.4 (*)
|    |    +--- org.springframework:spring-aop:6.1.13 (*)
|    |    \--- org.aspectj:aspectjweaver:1.9.22.1
|    +--- org.springframework.boot:spring-boot-starter-jdbc:3.3.4
|    |    +--- org.springframework.boot:spring-boot-starter:3.3.4 (*)
|    |    +--- com.zaxxer:HikariCP:5.1.0
|    |    |    \--- org.slf4j:slf4j-api:1.7.36 -> 2.0.0-alpha5
|    |    \--- org.springframework:spring-jdbc:6.1.13
|    |         +--- org.springframework:spring-beans:6.1.13 (*)
|    |         +--- org.springframework:spring-core:6.1.13 (*)
|    |         \--- org.springframework:spring-tx:6.1.13
|    |              +--- org.springframework:spring-beans:6.1.13 (*)
|    |              \--- org.springframework:spring-core:6.1.13 (*)
|    +--- org.hibernate.orm:hibernate-core:6.5.3.Final
|    |    +--- jakarta.persistence:jakarta.persistence-api:3.1.0
|    |    \--- jakarta.transaction:jakarta.transaction-api:2.0.1
|    +--- org.springframework.data:spring-data-jpa:3.3.4
|    |    +--- org.springframework.data:spring-data-commons:3.3.4
|    |    |    +--- org.springframework:spring-core:6.1.13 (*)
|    |    |    +--- org.springframework:spring-beans:6.1.13 (*)
|    |    |    \--- org.slf4j:slf4j-api:2.0.2 -> 2.0.0-alpha5
|    |    +--- org.springframework:spring-orm:6.1.13
|    |    |    +--- org.springframework:spring-beans:6.1.13 (*)
|    |    |    +--- org.springframework:spring-core:6.1.13 (*)
|    |    |    +--- org.springframework:spring-jdbc:6.1.13 (*)
|    |    |    \--- org.springframework:spring-tx:6.1.13 (*)
|    |    +--- org.springframework:spring-context:6.1.13 (*)
|    |    +--- org.springframework:spring-aop:6.1.13 (*)
|    |    +--- org.springframework:spring-tx:6.1.13 (*)
|    |    +--- org.springframework:spring-beans:6.1.13 (*)
|    |    +--- org.springframework:spring-core:6.1.13 (*)
|    |    +--- org.antlr:antlr4-runtime:4.13.0
|    |    +--- jakarta.annotation:jakarta.annotation-api:2.0.0 -> 2.1.1
|    |    \--- org.slf4j:slf4j-api:2.0.2 -> 2.0.0-alpha5
|    \--- org.springframework:spring-aspects:6.1.13
|         \--- org.aspectj:aspectjweaver:1.9.22.1
+--- org.springframework.boot:spring-boot-starter -> 3.3.4 (*)
+--- org.springframework.boot:spring-boot-starter-jdbc -> 3.3.4 (*)
+--- org.springframework.boot:spring-boot-starter-aop -> 3.3.4 (*)
+--- org.springframework.boot:spring-boot-starter-actuator -> 3.3.4
|    +--- org.springframework.boot:spring-boot-starter:3.3.4 (*)
|    +--- org.springframework.boot:spring-boot-actuator-autoconfigure:3.3.4
|    |    +--- org.springframework.boot:spring-boot-actuator:3.3.4
|    |    |    \--- org.springframework.boot:spring-boot:3.3.4 (*)
|    |    +--- org.springframework.boot:spring-boot:3.3.4 (*)
|    |    \--- org.springframework.boot:spring-boot-autoconfigure:3.3.4 (*)
|    +--- io.micrometer:micrometer-observation:1.13.4 (*)
|    \--- io.micrometer:micrometer-jakarta9:1.13.4
|         +--- io.micrometer:micrometer-core:1.13.4
|         |    +--- io.micrometer:micrometer-commons:1.13.4
|         |    \--- io.micrometer:micrometer-observation:1.13.4 (*)
|         +--- io.micrometer:micrometer-commons:1.13.4
|         \--- io.micrometer:micrometer-observation:1.13.4 (*)
+--- org.springframework.boot:spring-boot-starter-validation -> 3.3.4
|    +--- org.springframework.boot:spring-boot-starter:3.3.4 (*)
|    +--- org.apache.tomcat.embed:tomcat-embed-el:10.1.30
|    \--- org.hibernate.validator:hibernate-validator:8.0.1.Final
|         +--- jakarta.validation:jakarta.validation-api:3.0.2
|         +--- org.jboss.logging:jboss-logging:3.4.3.Final -> 3.4.2.Final
|         \--- com.fasterxml:classmate:1.5.1 -> 1.7.0
+--- org.springframework:spring-webmvc:6.1.13 (*)
+--- org.jboss.logging:jboss-logging:3.4.2.Final
+--- org.projectlombok:lombok:1.18.30
+--- com.oracle.ojdbc:ojdbc8:19.3.0.0
|    +--- com.oracle.ojdbc:ucp:19.3.0.0
|    +--- com.oracle.ojdbc:oraclepki:19.3.0.0
|    +--- com.oracle.ojdbc:osdt_cert:19.3.0.0
|    +--- com.oracle.ojdbc:osdt_core:19.3.0.0
|    +--- com.oracle.ojdbc:simplefan:19.3.0.0
|    |    \--- com.oracle.ojdbc:ons:19.3.0.0
|    \--- com.oracle.ojdbc:ons:19.3.0.0
+--- org.glassfish.jaxb:jaxb-runtime:2.3.2
|    +--- org.glassfish.jaxb:txw2:2.3.2 -> 4.0.5
|    +--- com.sun.istack:istack-commons-runtime:3.0.8 -> 4.1.2
|    +--- org.jvnet.staxex:stax-ex:1.8.1 -> 2.1.0
|    \--- com.sun.xml.fastinfoset:FastInfoset:1.2.16 -> 2.1.1
+--- com.jcraft:jsch:0.1.55
+--- pl.pojo:pojo-tester:0.7.6
|    +--- org.slf4j:slf4j-api:1.7.25 -> 2.0.0-alpha5
|    +--- org.apache.commons:commons-lang3:3.7 -> 3.14.0
|    +--- org.apache.commons:commons-collections4:4.1
|    +--- com.googlecode.combinatoricslib:combinatoricslib:2.1
|    \--- org.javassist:javassist:3.22.0-GA
+--- com.google.guava:guava:32.0.1-jre
|    +--- com.google.guava:failureaccess:1.0.1
|    +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
|    +--- com.google.code.findbugs:jsr305:3.0.2
|    +--- org.checkerframework:checker-qual:3.33.0
|    +--- com.google.errorprone:error_prone_annotations:2.18.0
|    \--- com.google.j2objc:j2objc-annotations:2.8
+--- org.everit.json:org.everit.json.schema:1.5.1
|    +--- org.json:json:20160810
|    +--- com.google.guava:guava:19.0 -> 32.0.1-jre (*)
|    \--- commons-validator:commons-validator:1.5.1
|         +--- commons-beanutils:commons-beanutils:1.9.2
|         \--- commons-digester:commons-digester:1.8.1
+--- commons-io:commons-io:2.11.0
+--- org.slf4j:slf4j-api:2.0.0-alpha5
+--- org.slf4j:slf4j-simple:2.0.0-alpha5
|    \--- org.slf4j:slf4j-api:2.0.0-alpha5
+--- org.aspectj:aspectjtools:1.9.7
+--- com.ibm.db2:jcc:11.5.6.0
+--- org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0
|    +--- org.springdoc:springdoc-openapi-starter-webmvc-api:2.6.0
|    |    \--- org.springdoc:springdoc-openapi-starter-common:2.6.0
|    |         +--- org.springframework.boot:spring-boot-autoconfigure:3.3.0 -> 3.3.4 (*)
|    |         \--- io.swagger.core.v3:swagger-core-jakarta:2.2.22
|    |              +--- org.apache.commons:commons-lang3:3.14.0
|    |              +--- org.slf4j:slf4j-api:2.0.9 -> 2.0.0-alpha5
|    |              +--- io.swagger.core.v3:swagger-annotations-jakarta:2.2.22
|    |              +--- io.swagger.core.v3:swagger-models-jakarta:2.2.22
|    |              +--- org.yaml:snakeyaml:2.2
|    |              \--- jakarta.validation:jakarta.validation-api:3.0.2
|    \--- org.webjars:swagger-ui:5.17.14
+--- io.springfox:springfox-bean-validators:2.9.2
|    +--- com.google.guava:guava:20.0 -> 32.0.1-jre (*)
|    +--- org.slf4j:slf4j-api:1.7.25 -> 2.0.0-alpha5
|    +--- org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE
|    |    \--- org.slf4j:slf4j-api:1.7.10 -> 2.0.0-alpha5
|    +--- org.springframework.plugin:spring-plugin-metadata:1.2.0.RELEASE
|    |    +--- org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE (*)
|    |    \--- org.slf4j:slf4j-api:1.7.10 -> 2.0.0-alpha5
|    \--- io.springfox:springfox-spi:2.9.2
|         \--- io.springfox:springfox-core:2.9.2
|              +--- com.google.guava:guava:20.0 -> 32.0.1-jre (*)
|              +--- org.slf4j:slf4j-api:1.7.25 -> 2.0.0-alpha5
|              +--- org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE (*)
|              \--- org.springframework.plugin:spring-plugin-metadata:1.2.0.RELEASE (*)
\--- org.mockito:mockito-core:3.7.0
     +--- net.bytebuddy:byte-buddy-agent:1.10.19 -> 1.14.19
     \--- org.objenesis:objenesis:3.1
spring-boot gradle migration java-21
1个回答
0
投票

不是您问题的完整解决方案,但我在使用 Spring 3.3.5 时遇到了同样的问题。即使只是遵循 Spring 指南也无法在示例应用程序上工作。如果你降级到 3.3.3 就可以了。

但这并不能解释 Spring Boot 3.3.4 及以后的不兼容版本是什么

© www.soinside.com 2019 - 2024. All rights reserved.