我们知道对继承了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="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- default-lazy-init="true">