Hibernate 4和Spring 3的文件配置

3

我从Spring 3 Hibernate 4新技术开始学习,并尝试按照教程完成以下示例,但每次都遇到相同的错误。我使用的是Spring 3.1.1和Hibernate 4.1.0。

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<!-- Data Source Declaration -->
    <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql:Bibliotheque" />
        <property name="username" value="postgres" />
        <property name="password" value="root" />

    </bean>

    <!-- Session Factory Declaration -->
    <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.ardia.Adherent</value>
                <value>com.ardia.Emprunt</value>
                <value>com.ardia.Fournisseur</value>
                <value>com.ardia.Livre</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
<!-- Transaction Manager is defined -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       <property name="sessionFactory" ref="SessionFactory"/>
    </bean>

<!-- Enable the configuration of transactional behavior based on annotations -->

 <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

这是一条消息,我不理解:

处理XML 'org/springframework/transaction/interceptor/TransactionInterceptor' 时发生错误。有关更多详细信息,请参见错误日志。

处理 '/Hibernate_spring/src/application_configuration.xml' 时发生错误。

java.lang.NoClassDefFoundError: org/springframework/transaction/interceptor/TransactionInterceptor
    at org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser$AopAutoProxyConfigurer.configureAutoProxyCreator(AnnotationDrivenBeanDefinitionParser.java:126)
    at org.springframework.transaction.config.AnnotationDrivenBeanDefinitionParser.parse(AnnotationDrivenBeanDefinitionParser.java:84)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
    at org.springframework.ide.eclipse.beans.core.internal.model.namespaces.DelegatingNamespaceHandlerResolver$ElementTrackingNamespaceHandler.parse(DelegatingNamespaceHandlerResolver.java:177)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1335)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$ErrorSuppressingBeanDefinitionParserDelegate.parseCustomElement(BeansConfig.java:1292)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1325)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$2.registerBeanDefinitions(BeansConfig.java:389)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$2.loadBeanDefinitions(BeansConfig.java:375)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$3.call(BeansConfig.java:421)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$3.call(BeansConfig.java:1)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
3个回答

0

尝试在你的pom.xml文件中添加这个

<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>

这个aopalliance jar将你的事务管理器与你的Spring应用程序绑定。 - Sudipta

0

在你的类路径中应该有 spring-tx.jar 文件,这意味着它应该存在于你的应用程序的 (即WEB-INF/lib) 文件夹中。

检查一下你是否只有一个 spring-tx.jar,因为拥有多个相同类型的jar包也会导致 NoClassDefFoundError


我已经拥有了所有的Spring 3.1 jar包,并且创建了自己的库并附加到项目中。我检查了一下,发现其中有一个名为org.springframework.transaction-3.1.1.RELEASE.jar的事务jar包。 - FERESSS

0
请确保在classpath中拥有适当版本的spring-tx.jar、spring-orm.jar和hibernate-entitymanager.jar文件。
此外,请注意您的配置中transactionManager的引用是否正确。
<!-- An id of txManager is assigned here -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       <property name="sessionFactory" ref="SessionFactory"/>
    </bean>

<!-- While transactionManager is referenced here -->    
 <tx:annotation-driven transaction-manager="transactionManager"/>

这行配置应该将 transaction-manager 指向 txManager bean:

 <tx:annotation-driven transaction-manager="txManager"/>

我在我的库中有这个jar包org.springframework.transaction-3.1.1.RELEASE.jar,但我没有使用Maven。 - FERESSS
<!-- An id of txManager is assigned here --> <bean id="txmanager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="SessionFactory"/> </bean><!-- While transactionManager is referenced here --> <tx:annotation-driven transaction-manager="txmanager"/> - FERESSS
你的类路径上有spring-orm.jar和hibernate-entitymanager.jar吗? - Kevin Bowersox
我的新消息错误:处理XML时出错'org/aopalliance/intercept/MethodInterceptor'。请查看错误日志获取更多详细信息。 - FERESSS
现在你需要spring-aop.jar。 - Kevin Bowersox
@FERESSS 很高兴一切都正常了。如果它解决了您的问题,请考虑接受答案。 - Kevin Bowersox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接