ava.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is requir

ava.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required


异常信息:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException:

Error creating bean with name 'com.s2hs.manager.impl.UserManagerImpl#10b4b2f' defined in class path resource [applicationContext-commom.xml]: Initialization of bean failed;

nested exception is java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required

一看异常信息又是不能成功创建bean,并且还有熟悉的nested,原始就是你要初始化的这个类没有配sessionFactory或者hibernateTemplate属性,在spring配置文件中讲该bean(UserManagerImpl)配置一下即可:

<property name="sessionFactory" ref="sessionFactory"/>

注意:有的时候,也许你看配置文件都配置了,但是还是总是提示你以上异常,你也许会怀疑myeclipse的问题,或者自己太郁闷了,别闹心了,90%可能是你自己错了,见下面我整理别人的一个技术博客:

JavaEye blog:  http://lfw.javaeye.com/blog/225139

老是提示:sessionFactory or hibernateTemplate is required

让我很郁闷的是:我明明把sessionFactory注入进去了呀,怎么还是找不到的?

后来认真观察错误 Error creating bean with name 'com.lfw.sh.manager.impl.UserManagerImpl#10b4b2f' defined in class path resource

才发现原来是在creating UserManagerImpl类时找不到sessionFactory, 于是把

        <bean id="userManager" parent="baseTxService">  

    <property name="target">  
    <bean class="com.lfw.sh.manager.impl.UserManagerImpl" />  
    </property>  
    <property name="sessionFactory">  
    <ref bean="sessionFactory"/>  
    </property>  
    </bean>

    <bean id="userManager" parent="baseTxService"> 
            <property name="target"> 
                <bean class="com.lfw.sh.manager.impl.UserManagerImpl"> 
                     <property name="sessionFactory"> 
                        <ref bean="sessionFactory"/> 
                     </property> 
                </bean> 
            </property> 
             
        </bean>

这样就搞定了,后来经一位高人指点得知:我的com.lfw.sh.manager.impl.UserManagerImpl继承了org.springframework.orm.hibernate3.support.HibernateDaoSupport,spring在初始化容器的时候会对这个对象作一个check, 看下HibernateDaoSupport源码中的checkDaoConfig()方法 就清楚啦:

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

所以,在com.lfw.sh.manager.impl.UserManageImpl对象一定要注入一个sessionFactory or hibernateTemplate对象。

你可能感兴趣的:(ava.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is requir)