首先遇到的问题就是HibernateDaoSupport引起的,程序中所有的DAO都继承自HibernateDaoSupport,而HibernateDaoSupport需要注入sessionfactory或者hibernateTemplate,所以出现"sessionFactory " or "hibernateTemplate " is required异常,但是在spring配置文件中加入sessionFactory的bean配置以后,仍然出现异常。
后来看了网上的解决方式 ,原因是spring.xml中没有加上default-autowire="byName" ,在注解的时候找不到实例化的sessionFactory,而注入了一个空的,在hibernate检查的时候就报那个错了。spring配置文件加入byName的方式注入bean后,就可以正确使用注解了
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
总的原因就是缺少了这句default-autowire="byName"。这个鸟问题困惑了一下午
开始在没添加default-autowire="byName"的时候,我添加了如下代码就可以,不过这个只是使得找到问题出现的原因,并不是解决的最终方法,最终解决方法还是添加了上面的一句到配置文件中。
import com.yhqxgl.dao.TDeptDao;
import com.yhqxgl.entity.TDept;
@Repository("tDeptDaoImpl")
@Transactional
public class TDeptDaoImpl extends HibernateDaoSupport implements TDeptDao {
public boolean add(TDept dept) {
this.getHibernateTemplate().save(dept);
return true;
}
@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory)
{
super.setSessionFactory(sessionFactory);
}
}
相当sessionFactory还没注入到HibernateDaoSupport中,使得这里的this.getHibernateTemplate()=null。
分享到:
相关推荐
NULL 博文链接:https://tedtian.iteye.com/blog/1988917
Spring文件中的 SessionFactory中 加入为了能得到同一个Session
SSH的整合问题集中处理 <!-- 事务配置 --> <property name="sessionFactory" ref="sessionFactory" /> <!-- JTA环境的事务配置 ...
(S+S+H)(private_SessionFactory_属性_dao类)_方式_SSH_整合--总结
1. 公共类搭建 1.1 po对象及其映射 1.2util包工具类 ...3.7.编辑applicationContext.xml, dao注入sessionFactory,service注入dao,action注入service。实现Spring IoC配置 4 各模块具体实现 4.1 登录模块
-- 配置SessionFactory(整合Hibernate) --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 数据库连接信息 --> ${...
SSH框架整合录像 解决包冲突 sessionFactory 问题 project的命名经验 事务的配置
该项目只涉及到两个实体类 Administer 和 Book ,两者之间没什么联系...另外该项目严格按照View-Service-Dao三层,所有对象(包括Action,Service,Dao,SessionFactory、Transaction等对象)都交由Spring容器来创建。
主要介绍了SSH整合异常解决(creating bean with name 'sessionFactory' defined in class path)的相关资料,需要的朋友可以参考下
本人的深刻教训,希望后来者少犯错误!!这些低级,变态的配置,和变态的问题
ssh 框架整合的实例。 applicationContext.xml xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p=...
ssh框架搭建step by step (springMVC + spring 5.0.4 + hibernate 5.0.12) 好久不弄web了, 周末心血来潮, 使用较新spirng/hibernate搭建一个ssh框架, 供有需要的同学参考/学习/使用. 使用eclipse开发, 搭建,分三步: ...
Java ssh+oracle整合; <property name="username" value="scott"></property> <property name="password" value="tiger"></property> <bean id="sessionFactory" class="org.spring...
如:SessionFactory的注入、HibernateTemplate的简化操作、DAO的支持等。 为了更好地与持久层框架整合,Spring还提供了统一的异常处理体系和事务管理方法。 24.1 概述 24.1.1 概述 如果Spring与Hibernate进行了整合...
HibernateSessionFactory.java
Session s= HibernateSessionFactory.getSession(); 就是Hibernate的工具java类
hibernate中的SessionFactoryhibernate中的SessionFactoryhibernate中的SessionFactory
NULL 博文链接:https://yao37duman.iteye.com/blog/1724375
javaEE SSH三大框架整合,Spring整合Hibernate,将Hibernate的SessionFactory对象交给Spring容器管理(方案一,不推荐)-附件资源