我把Spring和Hibernate整合起来时,遇到个错误搞了好久,按照网上的方法也没法解决
抛出错误如下:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.lfw.sh.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
java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required
Spring配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="88888888"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingResources"> <list> <value>com/lfw/sh/domin/User.hbm.xml</value> </list> </property></bean> <!--Hibernate TransactionManager--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--Base TransactionProxyed Service Bean--> <bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"/> <property name="proxyTargetClass" value="true"/> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="updateBillSeq*">PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="submit*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <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> </beans>
老是提示: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对象。