当前位置: 代码迷 >> J2EE >> Spring 使用注解配置事物时出现:'sessionFactory' or 'hibernateTemplate' is required,该怎么解决
  详细解决方案

Spring 使用注解配置事物时出现:'sessionFactory' or 'hibernateTemplate' is required,该怎么解决

热度:682   发布时间:2016-04-22 01:59:36.0
Spring 使用注解配置事物时出现:'sessionFactory' or 'hibernateTemplate' is required
spring 配置文件如下:
XML code
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-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">        <context:annotation-config/>    <context:component-scan base-package="com.unmi.dao"/>    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>        <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation"    value="file:src/hibernate.cfg.xml"/>        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>    </bean>        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory">            <ref bean="sessionFactory"/>        </property>    </bean></beans>


PersonDaoImpl 代码声明(加了注释)如下:
Java code
@Transactional@Component("personDao")public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{...}


客户端代码如下:
Java code
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");                PersonDao personDao = (PersonDao)beanFactory.getBean("personDao");                Person person = new Person("Unmi",23,"OK,Fu");                personDao.save(person);


执行后出现异常:
2010-04-15 13:35:43,765 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[INFO] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@667cbde6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,personDao,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,sessionFactory,transactionManager]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personDao' defined in file [E:\workspace\TestSpring\bin\com\unmi\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
  相关解决方案