Spring Boot 3.0.6 Native Image启动失败:entityManagerFactory实例化异常
问题描述
- 使用Spring Boot 3.0.6版本,依赖包含
spring-boot-starter-data-jpa和spring-boot-starter-web - 通过命令
mvn spring-boot:build-image -Pnative构建Native Image镜像 - 启动镜像时抛出Bean创建异常,核心错误为创建名为
entityManagerFactory的Bean失败,根因:java.lang.IllegalStateException: No available JtaPlatform candidates amongst [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform, org.hibernate.service.jta.platform.internal.NoJtaPlatform] - 完整报错栈:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Instantiation of supplied bean failed at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1220) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1158) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1132) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:907) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[com.example.demo.NativeServiceApplication:3.0.6] at com.example.demo.NativeServiceApplication.main(NativeServiceApplication.java:10) ~[com.example.demo.NativeServiceApplication:na] Caused by: java.lang.IllegalStateException: No available JtaPlatform candidates amongst [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform, org.hibernate.service.jta.platform.internal.NoJtaPlatform] at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getNoJtaPlatformManager(HibernateJpaConfiguration.java:213) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.configureJtaPlatform(HibernateJpaConfiguration.java:150) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.customizeVendorProperties(HibernateJpaConfiguration.java:139) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:132) ~[com.example.demo.NativeServiceApplication:3.0.6] at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration__BeanDefinitions.lambda$getEntityManagerFactoryInstanceSupplier$3(JpaBaseConfiguration__BeanDefinitions.java:84) ~[na:na] at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$get$2(BeanInstanceSupplier.java:204) ~[na:na] at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.aot.BeanInstanceSupplier.invokeBeanSupplier(BeanInstanceSupplier.java:216) ~[na:na] at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:204) ~[na:na] at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:947) ~[com.example.demo.NativeServiceApplication:6.0.8] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1214) ~[com.example.demo.NativeServiceApplication:6.0.8] ... 17 common frames omitted
解决方案
方法1:显式指定Hibernate的JtaPlatform
在配置文件中直接指定JtaPlatform实现,避免自动检测失败:
application.properties
spring.jpa.properties.hibernate.transaction.jta.platform=org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform
application.yml
spring: jpa: properties: hibernate: transaction: jta: platform: org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform
方法2:添加Hibernate Native镜像支持依赖
添加Hibernate针对Native镜像优化的依赖,确保必要类被正确打包:
<dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> <classifier>native-image</classifier> <scope>runtime</scope> </dependency>
方法3:提前生成AOT配置
先执行AOT配置生成命令,确保Native镜像构建时能获取到正确的Bean元数据:
mvn spring-boot:aot-generate mvn spring-boot:build-image -Pnative
内容的提问来源于stack exchange,提问作者yora
相关产品推荐
相关产品推荐

