Error creating bean with name 'userDao'

本文探讨了在使用Spring框架和Hibernate时遇到的初始化错误问题,特别是当尝试使用Annotation进行依赖注入时遇到的挑战。文章详细解释了错误产生的原因,即Spring对sessionFactory的管理方式与Annotation管理不兼容,以及提供了两种解决方案来解决这个问题。

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

[color=red]严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.lj.service.IUserService com.lj.controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.lj.dao.UserDao.setSessionFactory(org.hibernate.SessionFactory); nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;[/color]

在启动的时候,会报出一堆这样的错误。 所有我们使用Annotation的注入都会被报错。

出现这个错误的原因是Spring对sessionFactory的管理。

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">


这里我们使用了hibernate3.LocalSessionFactoryBean,
但是这个类是不支持对Annotation进行管理的。
也就是说我们没有使用mapping文件比如User.xml文件来进行映射的管理。

这里的解决方法有两种,

1: orm.hibernate3.LocalSessionFactoryBean改成orm.hibernate3.annotation.AnnotationSessionFactoryBean,
后者支持hibernate通过Annotation管理映射。

2: 改成orm.hibernate4.LocalSessionFactoryBean.
这里貌似要使用hibernate4才可以。

总之如果容器启动就看到一堆初始化错误,注入异常的信息,很可能就是这个原因了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值