错误:Error creating bean with name 'sessionFactory' defined in class path resource

本文针对Spring整合Hibernate时遇到的SessionFactory创建失败问题进行了解析。通过分析控制台错误日志,定位到是由Hibernate版本不匹配引起的ClassNotFoundException。文章给出了具体的解决方案,包括正确配置版本及导入合适的jar包。

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

问题:在Spring整合Hibernate时,出现“SessionFactory 创建不成功,初始化失败”的错误。

查了百度后发现,造成这个错误有很多原因,比如什么数据库映射文件写错呀、引用路径些写错等各种错误层出不穷。后来自己经过思考发现,其实只要你的系统通过监听器在服务器启动时就加载Spring的配置文件,从而创建sessionfactory对象的过程中有错误就会报这个初始化文件的错误。所以说一旦别人的解决办法不能解决自己的问题时,就证明你的错误跟他的不一样,这是就需要自己在去看eclipse的控制台显示的错误日志中的caused by后面的信息,看具体由什么原因造成。这也就是我通过这个题目给自己的反思。今后一定要多仔细观察控制台中信息,这样才能提高自己解决错误的能力。

回到正题,我查看控制台后面的信息后发现了这么一句:java.lang.ClassNotFoundException: org.hibernate.boot.model.naming.ImplicitNamingStrategy。经差,是由于hibernate版本问题导致出现这个问题。你在spring配置文件中创建sessionfactory对象时的ckass路径需要跟你使用的版本一致,而且是要使用hibernate5版本的jar包,如果使用低版本的jar,配置文件中的具体将会不一样(具体不做说明,知道是这个原因就行)。我先是去hiberbate官网把版本5下载了下来,然后将核心jar包导入后在运行就可以了。

2025-04-24T16:15:04.338+08:00 ERROR 3736 --- [ restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService': Error creating bean with name 'userDetailsServiceImpl': Unsatisfied dependency expressed through field 'iUserService': Error creating bean with name 'IUserServiceImpl': Unsatisfied dependency expressed through field 'baseMapper': Error creating bean with name 'userMapper' defined in file [D:\dm\Ssss\back\target\classes\cn\zwz\data\dao\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Generic.class]: Unsatisfied dependency expressed through method 'dataSource' parameter 0: Error creating bean with name 'spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties': Could not bind properties to 'DataSourceProperties' : prefix=spring.datasource, ignoreInvalidFields=false, ignoreUnknownFields=true
最新发布
04-25
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapperConfigurer' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authorizationAttributeSourceAdvisor' defined in class path resource [com/ruoyi/framework/config/ShiroConfig.class]: Unsatisfied dependency expressed through method 'authorizationAttributeSourceAdvisor' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shiroFilterFactoryBean' defined in class path resource [com/ruoyi/framework/config/ShiroConfig.class]: Unsatisfied dependency expressed through method 'shiroFilterFactoryBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityManager' defined in class path resource [com/ruoyi/framework/config/ShiroConfig.class]: Unsatisfied dependency expressed through method 'securityManager' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRealm': Unsatisfied dependency expressed through field 'menuService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sysMenuServiceImpl': Unsatisfied dependency expressed through field 'menuMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sysMenuMapper' defined in URL [jar:file:/opt/tongweb7047/deployment/jsb_agriculture/WEB-INF/lib/ruoyi-system-4.2.0.jar!/com/ruoyi/system/mapper/SysMenuMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/ruoyi/framework/config/MyBatisConfig.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [masterDataSource, dynamicDataSource, gaussdbDataSource, oracleDataSource]
04-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值