'sessionFactory' or 'hibernateTemplate' is required解决方法
这种情况就是在通过spring配置hibernate4的时候(注意,这里是hibernate4不是hibernate3,hibernate3的),使用的是HibernateDaoSupport的这种方式:
public class BaseDao extends HibernateDaoSupport{
*****
}
类似这种方式,然后在applicationContext.xml配置为:
***
<!-- 配置 SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties" ref="hibernate-hibernateProperties"></property>
<property name="packagesToScan" ref="hibernate-packagesToScan"></property>
<property name="mappingLocations" ref="hibernate-mappingLocations"></property>
</bean>
<bean id="baseDao" class="com.myprj.auth.dao.BaseDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
结果一运行会报:
'sessionFactory' or 'hibernateTemplate' is required 这种错误
结果查看HibernateDaoSupport的源代码得知:setSessionFactory方法是:
public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
}
说明在子类中想通注解设置 sessionFactory (重写setter方法是不行的),那么怎么办,我们只能通过super.setSessionFactory ()调用
解决办法:
@Resource(name="sessionFactory")
private void setMySessionFactory(SessionFactory sessionFactory){
//这个方法名可以随便写,@Resource可以通过name 或者type来装载的。
super.setSessionFactory(sessionFactory);
}
所以最终可以发现在applicationContext.xml配置文件中的这个bean:
<bean id="baseDao" class="com.myprj.auth.dao.BaseDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
是没用的
但是,注意,这里是hibernate4不是hibernate3,hibernate3是可以这样配置的,在使用hibernate3的spring sessionFactory中可以通过在applicationContext.xml文件中:
<bean id="baseDao" class="com.myprj.auth.dao.BaseDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
然后直接写:
public class BaseDao extends HibernateDaoSupport{
*****
}就可以
'sessionFactory' or 'hibernateTemplate' is required解决方法的更多相关文章
- spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法
做单元测试的时候,抛出异常 Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' ...
- Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required
今日在写GenericDao时,发现了一个异常,内容如下: org.springframework.beans.factory.BeanCreationException: Error creatin ...
- java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required 严重: Exceptio ...
- SSH整合,"sessionFactory " or "hibernateTemplate " is required异常
首先遇到的问题就是HibernateDaoSupport引起的,程序中所有的DAO都继承自HibernateDaoSupport,而HibernateDaoSupport需要注入sessionfact ...
- SSH框架环境搭建问题:java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
SSH框架启动tomcate时出错 严重: Exception sending context initialized event to listener instance of class org. ...
- 安装Scrapy报错 error: Microsoft Visual C++ 14.0 is required解决方法
[问题背景]:在Windows 10系统,pip install Scrapy,报错error: Microsoft Visual C++ 14.0 is required,还有提示Twisted需要 ...
- python库安装中Microsoft Visual C++ is required解决方法
在用pycharm过程中,用pip去安装一些第三方包的时候会出现如下错误,缺少C++编译器,因为有些程序需要使用,没有C++接口会报错,查阅相关资料及自己的解决方案 error: Microsoft ...
- pip安装scrapy失败:twisted安装失败 error: Microsoft Visual C++ 14.0 is required.. 解决方法
在使用pip install scrapy命令安装scrapy框架时,Twisted出现安装错误.如下: building 'twisted.test.raiser' extension error: ...
- Eclipse is running in a JRE, but a JDK is required 解决方法(转)
转自:http://comeonbabye.iteye.com/blog/1186239 安装Maven后每次启动出现警告信息: Eclipse is running in a JRE, but a ...
随机推荐
- redis——持久化篇
众所周知,redis是内存数据库,它把数据存储在内存中,这样在加快读取速度的同时也对数据安全性产生了新的问题,即当redis所在服务器发生宕机后,redis数据库里的所有数据将会全部丢失. 为了解决这 ...
- mysql定时器Events
MySQL定时器Events 一.背景 我们MySQL的表A的数据量已经达到1.6亿,由于一些历史原因,需要把表A的数据转移到一个新表B,但是因为这是线上产品,所以宕机时间需要尽量的短,在不影响数据持 ...
- eclipse中的代码提示功能
Eclipse 的代码提示功能,具体配置 1. 打开Eclipse ,然后"window"→"Preferences" 2. 选择"java" ...
- WinForm richtextbox 关键字变红色
private void HilightRichText(RichTextBox control, string hilightString) { int nSel ...
- [转载]赖勇浩:推荐《Linux 多线程服务器端编程》
推荐<Linux 多线程服务器端编程> 赖勇浩(http://laiyonghao.com) 最近,有一位朋友因为工作需要,需要从网游的客户端编程转向服务器端编程,找我推荐一本书.我推荐了 ...
- final、finally、finalize的区别
看C#知识点的时候发现的问题 1.final 修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被声明为 abstract的,又被声明为f ...
- spring 事务:注解方式
(1) .<context:component-scan base-package="*.*" /> 该配置隐式注册了多个对注解进行解析的处理器,如: Autowire ...
- 框架介绍thinkphp
ThinkPHP是一个免费开源的,快速.简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的.ThinkPH ...
- 一个Angular模块中可以声明哪些组件?
一个Angular模块中可以声明哪些组件? (1) controller 控制器 (2) directive 指令 (3) function ...
- Spark如何使用Akka实现进程、节点通信的简明介绍
<深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...