spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法

时间:2024-01-19 15:15:47

做单元测试的时候,抛出异常

Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:118)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
... 39 more

 整合中对于dao是如下:自定义基类dao接口类IBaseDao<T>,BaseDaoImpl实现IBaseDao,继承HibernateDaoSupport。注解方式,需要对sessionFactory进行注入,

在BaseDaoImpl中添加:

@Resource
public final void setMySessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}