2 lv qinyu lv_qinyu 于 2017.01.11 22:39 提问

关于SSH中applicationContext.xml配置问题

小弟在在配置applicationContext.xml文件时报错如下

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at StoreOfAPUTest.insertTest(StoreOfAPUTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:267)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:231)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl$SessionFactoryOptionsStateStandardImpl.interpretConnectionHandlingMode(SessionFactoryBuilderImpl.java:820)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl$SessionFactoryOptionsStateStandardImpl.<init>(SessionFactoryBuilderImpl.java:723)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.<init>(SessionFactoryBuilderImpl.java:80)
    at org.hibernate.boot.internal.MetadataImpl.getSessionFactoryBuilder(MetadataImpl.java:135)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:693)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:863)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:782)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
    ... 35 more
Caused by: java.lang.ClassCastException: org.springframework.orm.hibernate3.SpringTransactionFactory cannot be cast to org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder
    at org.hibernate.resource.transaction.internal.TransactionCoordinatorBuilderInitiator.initiateService(TransactionCoordinatorBuilderInitiator.java:35)
    at org.hibernate.resource.transaction.internal.TransactionCoordinatorBuilderInitiator.initiateService(TransactionCoordinatorBuilderInitiator.java:25)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:257)
    ... 48 more

applicationContext.xml问价如下:

 <?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="hettp://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/spring-aop.xsd"> -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">
 <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean> 
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="StoreOfAPU" class="cn.itcast.dao.impl.StoreOfAPUDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
    <bean id="StoreOfAPUService" class="cn.itcast.service.impl.StoreOfAPUServiceImpl">
        <property name="StoreOfAPU" ref="StoreOfAPU">
        </property>
    </bean>
</beans>

说是sessionFactory定义有问题,sessionFactory定义有问题,但是我不知道问题出在哪里了。
org.springframework.orm中有org.springframework.orm.hibernate3.LocalSessionFactoryBean这个,所以我就一脸懵逼,求前辈们帮帮我,谢谢!

2个回答

JE_GE
JE_GE   2017.01.11 23:42
 <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean> 
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

版本orm.hibernate3,orm.hibernate4不能混用

lv_qinyu
lv_qinyu 这个不小心错了,开始都是3,好像hibernat4里面去掉了hibernateTemplate这个类,改了那里还是有问题
一年多之前 回复
JE_GE
JE_GE   2017.01.20 16:58
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
ssh架构配置文件配置
关于在ssh项目开发中 ,applicationcontext.xml 的配置
SSH框架中applicationContext.xml理解
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.
spring2.0学习的案例
spring中的配置问题,包,实例,applicationContext.xml
SSH框架中applicationContext.xml文件的配置
今天我给大家详细解释一下Spring的applicationContext.xml文件。Ok,还是通过代码加注释的方式为大家演示; 以下是详解Spring的applicationContext.xml文件代码: 1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3
SSH配置文件之applicationContext.xml
在SSH开发中,applicationContext.xml的配置文件基本都差不多的,差不多都是以下格式:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in
初识SSH2——applicationContext.xml基础配置
(由于实在是被2本800多页的SSH框架学习资料吓到之后,开始了‘投机取巧’的学习过程) 于是挑着看了资料书和网上关于struts2、spring、hibernate核心简介的内容,在了解这三个框架各自的工作流程之后,直接大胆的开始学习SSH框架整合(虽然网上资料太多太杂但多找几篇并对比学习还是能迅速掌握)。 在与各种Error打了一整天交道之后,终于整合出了第一个启动Tomcat无错的环境,
SSH整合配置applicationContext.xml/hibernate.cfg.xml/struts.xml
SSH整合配置applicationContext.xml/hibernate.cfg.xml/struts.xml,本人在这里的Bean使用自动扫描与装配;使用注解方式管理事务;并且把Hibernate中的数据源信息放在了这里。另外把JDBC的数据连接信息单独放在了一个名叫 jdbc.properties 中,然后引用外部文件$方法调用;(这样有利于未来更换数据库时只要单独修改 jdbc.properties即可)
SSH中action、applicationContext.xml和struts.xml中各种对应
在struts.xml中 class对应的是applicationContext-action.xml中bean的id。 applicationContext-action.xml的bean中的property的name与action类中声明的私有对象名是对应的。 例如,在action类中声明了一个private StudentService studentService;那么这个studen
SSH整合之applicationContext.xml配置文件
SSH整合中applicationContext.xml配置文件有两种写法:&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt; &amp;lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; ...
搭建SSH框架的时候的applicationContext.xml配置文件
 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/schem