阅读780 返回首页    go 阿里云 go 技术社区[云栖]


SSH整合时sessionFactory or hibernateTemplate is required异常

 

我们知道对继承了org.springframework.orm.hibernate3.support.HibernateDaoSupport的类来说,需要注入sessionFactory呢?因为spring在初始化容器的时候会对这个对象作一个check, 看下HibernateDaoSupport源码中的checkDaoConfig()方法 就清楚啦:

protected final void checkDaoConfig()
{      
    if (this.hibernateTemplate == null)
    {      
        throw new IllegalArgumentException
            ("'sessionFactory' or 'hibernateTemplate' is required");    
    }      

 

但是在spring配置文件中加入sessionFactory的bean配置以后,仍然出现异常。

后来看了网上的解决方式,原因是spring.xml中没有加上default- ,在注解的时候找不到实例化的sessionFactory,而注入了一个空的,在hibernate检查的时候就报那个错了。spring配置文件加入byName的方式注入bean后,就可以正确使用注解了

<beans
    xmlns="https://www.springframework.org/schema/beans"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="https://www.springframework.org/schema/aop"   
    xmlns:tx="https://www.springframework.org/schema/tx" 
    xmlns:context="https://www.springframework.org/schema/context"  
    xsi:schemaLocation="    
                https://www.springframework.org/schema/beans     
                https://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
                https://www.springframework.org/schema/aop     
                https://www.springframework.org/schema/aop/spring-aop-2.5.xsd    
                https://www.springframework.org/schema/tx     
                https://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                https://www.springframework.org/schema/context
                https://www.springframework.org/schema/context/spring-context-2.5.xsd"
                default- default-lazy-init="true">

最后更新:2017-04-02 06:52:16

  上一篇:go SQL FOR XML子句的各种用法
  下一篇:go excel 查一列字符是否在另一列中出现