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