Spring boot Jpa异常 : Error creating bean with name 'xxx' defined in class path resource

本文探讨了在Spring Boot应用中配置JPA时常见的错误,具体分析了一个关于数据源配置缺失导致EntityManagerFactory初始化失败的问题。通过示例代码展示了正确的数据源配置方式,强调了在使用DataSourceBuilder创建数据源时,正确设置数据库连接参数的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/itnull/xunwu/config/JPAConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
   

问题原因:数据源配置项未获取,多数都是配置问题。

    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {

//       return DataSourceBuilder.create().driverClassName("com.mysql.cj.jdbc.Driver")
//                                        .password("root")
//                                        .username("root")
//               .url("jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=GMT&useSSL=false")
//               .build();
        return DataSourceBuilder.create().build();

    }
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceContext' defined in class path resource [conf/service-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssGenClientKeyService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.security.util.Conf cn.com.higinet.security.service.ext.GenClientKeyService.ssConf; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssconf': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.rapid.base.dao.BaseDao cn.com.higinet.security.util.Conf.essSSBaseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.higinet.rapid.base.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93) at cn.com.higinet.rapid.server.Bootstrap.main(Bootstrap.java:60) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssGenClientKeyService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.security.util.Conf cn.com.higinet.security.service.ext.GenClientKeyService.ssConf; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssconf': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.rapid.base.dao.BaseDao cn.com.higinet.security.util.Conf.essSSBaseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.higinet.rapid.base.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079) at cn.com.higinet.rapid.server.core.ServiceContext.setApplicationContext(ServiceContext.java:39) at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:106) at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:85) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1413) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ... 11 more Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.security.util.Conf cn.com.higinet.security.service.ext.GenClientKeyService.ssConf; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssconf': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.rapid.base.dao.BaseDao cn.com.higinet.security.util.Conf.essSSBaseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.higinet.rapid.base.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282) ... 25 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ssconf': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.rapid.base.dao.BaseDao cn.com.higinet.security.util.Conf.essSSBaseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.higinet.rapid.base.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:844) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:786) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474) ... 27 more Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.higinet.rapid.base.dao.BaseDao cn.com.higinet.security.util.Conf.essSSBaseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.higinet.rapid.base.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282) ... 38 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.higinet.rapid.base.dao.BaseDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474) ... 40 more
06-11
### 解决 `entityManagerFactory` Bean 创建失败的方法 当遇到创建名为 `entityManagerFactory` 的 bean 失败时,通常是因为配置文件中的属性未能正确绑定到相应的类上或者存在类型转换问题。Spring 尝试将外部应用程序属性强制转换为正确的类型并将其绑定到带有 `@ConfigurationProperties` 注解的 beans 上[^1]。 如果默认的类型转换机制无法满足需求,则可以提供自定义类型的转换服务来处理特定的数据映射逻辑: - 提供一个具有 bean ID 为 `conversionService` 的 `ConversionService` 实现; - 使用带 `CustomEditorConfigurer` 注解的 bean 来注册自定义编辑器; - 定义带有 `@ConfigurationPropertiesBinding` 注解的 converter 类型。 对于具体的 `entityManagerFactory` 错误排查建议如下: #### 配置检查 确认 persistence.xml 或者 spring boot 应用程序属性中关于数据源、JPA 设置等是否正确无误,并且这些设置能够被 Spring Boot 正确加载和解析。 #### 日志分析 查看详细的异常堆栈跟踪信息,定位具体哪一部分初始化过程中出现了问题。常见的原因可能包括但不限于: - 数据库连接池参数不匹配目标数据库服务器的要求。 - Hibernate 特定版本与使用的其他依赖之间兼容性冲突。 - Entity 所需表结构不存在于指定 schema 中。 通过以上方法应该可以帮助识别并修复 `entityManagerFactory` bean 创建过程中的错误。 ```java @Bean(name = "entityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) { return builder .dataSource(dataSource) .packages("com.example.model") // Specify entity packages here .persistenceUnit("default") .build(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值