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

Spring Boot构建时创建entityManagerFactory Bean报错求助

解决Spring Boot 2.0 + Java 9环境下JAXBException的问题

这个问题我之前踩过坑!你猜得不对,根源不是Spring Data JPA或者H2的版本不匹配,而是Java 9的模块化变化导致的——JAXB API在Java 9之后被移出了JDK的默认模块,而你用的Spring Boot 2.0.0版本的starter并没有自动包含这部分依赖,所以启动时会找不到javax.xml.bind.JAXBException类。

下面给你两种可行的解决方案:

方案一:手动添加JAXB相关依赖

直接在你的Gradle依赖中补充JAXB的API和实现类,修改dependencies块如下:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    // 新增JAXB依赖,解决Java 9兼容性问题
    compile('javax.xml.bind:jaxb-api:2.3.1')
    compile('com.sun.xml.bind:jaxb-core:2.3.0.1')
    compile('com.sun.xml.bind:jaxb-impl:2.3.2')
    compile('javax.activation:activation:1.1.1')
    runtime('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

添加完后执行gradle clean build刷新依赖,再启动项目应该就能正常运行了。

方案二:升级Spring Boot版本(更推荐)

Spring Boot 2.1及以上版本已经原生支持Java 9+的模块化,会自动处理JAXB这类依赖的引入。你可以把buildscript里的Spring Boot版本升级到2.1系列的稳定版,比如:

buildscript {
ext {
    springBootVersion = '2.1.18.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

升级后不需要手动添加任何额外依赖,Spring Boot会自动帮你管理好这些兼容细节,还能顺便解决其他可能存在的Java 9兼容性问题。

两种方案选一个执行后,重新构建并启动项目,这个BeanCreationException应该就消失了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:18:25