Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required

  今日在写GenericDao时,发现了一个异常,内容如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘genericDao‘ defined in class path resource [spring.common.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: ‘sessionFactory‘ or ‘hibernateTemplate‘ is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.oliver.test.UserDaoTest.setUp(UserDaoTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: ‘sessionFactory‘ or ‘hibernateTemplate‘ is required
at org.springframework.orm.hibernate4.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
... 35 more

这个异常产生的原因是因为在GenericDaoImpl中使用了HibernateDaoSupport,但是并没有注入sessionFactory对象。

在经过了搜索寻找原因后,发现主要的解决方案都是在Dao中增加了一个方法,来给HibernateDaoSupport设置sessionFacotory。

这一步是必须的。

而接下来在spring中配置了default-autowire="byName"。我认为这样不太好,这种方式也很少用。

因此可以使用<context:annotation-config/>配置来完成初始化。

好了,这样就成功的解决了这个异常。

Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required

时间: 2024-10-13 06:37:34

Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required的相关文章

java.lang.IllegalArgumentException: &#39;sessionFactory&#39; or &#39;hibernateTemplate&#39; is required

java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.facto

hibernate整合spring事务异常

Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. 该段错误的原因是在保存或者更新数据的时候事务异常,检查一下service里面的方法名是否符合spring配置文件里面的事务提交规则.

ssh整合之三hibernate和spring整合

1.拷贝我们的spring事务控制所需的jar包 2.在spring容器中配置我们的hibernateTemplate以及事务管理器 <?xml version="1.0" encoding="UTF-8"?> <!-- spring的配置文件:导入约束 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt

Spring与Hibernate整合,实现Hibernate事务管理

1.所需的jar包 连接池/数据库驱动包 Hibernate相关jar Spring 核心包(5个) Spring aop 包(4个) spring-orm-3.2.5.RELEASE.jar                 [spring对hibernate的支持] spring-tx-3.2.5.RELEASE.jar                     [事务相关] 2.配置文件 Product.hbm.xml <?xml version="1.0" encoding=

Spring与Hibernate整合

Spring与Hibernate整合 Spring与Hibernate整合关键点: 1) Hibernate的SessionFactory对象交给Spring创建:   SessionFactory即是hibernate.cfg.xml配置文件 2) hibernate事务交给spring的声明式事务管理. SSH整合: Spring与Struts: Spring与hibernate整合: SH整合步骤: 1)引入jar包 连接池/数据库驱动包   spring对c3p0连接池的支持比hiber

hibernateTemplate不建议在Spring与Hibernate整合中使用

HibernateTemplate类属于spring框架中的类 :org.springframework.orm.hibernate3.HibernateTemplate HibernateTemplate类目前Hibernate4不支持此类,只支持到Hibernate3,而且在SH整合过程中,如果DaoImpl采用Hibernate实现方法,如果采用Hibernate原声的SessionFactory,即使不采用Spring,原声的SessionFaction同样可以使用,将有利于Dao是可以

spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory

Hibernate 3.6以上版本在用junit测试时会提示错误: Unable to get the default Bean Validation factory spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory  ,是因为新版hibernate用到新的jar包造成的,默认会自动找验证包,吴国不需要这一步,可以在spring整合hibernate的配置节点中添加如下标红属性: <bean id="sessio

Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题

最近在知乎看到一句话,保持学习的有一种是你看到了更多的牛人,不甘心,真的不甘心. Spring和hibernate整合的时候,jsp页面做展现,发现展现属性出现: org.apache.jasper.JasperException: could not initialize proxy - no Session - Class: org.hibernate.proxy.AbstractLazyInitializer File: AbstractLazyInitializer.java no se

【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明

摘要 在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍. 1. sessionFactory和数据库对应,有多少个数据库,就需要配置多少个sessionFactory: 2. session相当于数据库连接,进行数据库的CRUD操作时,需要开启session,用完需要关闭session: 3. 配置sessionFactory,主要要配置如下三个方面: 3.1. 数据源配置: 3.