Spring配置和事务

时间:2016-04-26 19:21:20

标签: java spring hibernate

我玩了很多次,改变了默认的目标命名空间,仍然会抱怨上下文未声明的元素:Multiple annotations found at this line: - cvc-elt.1: Cannot find the declaration of element'context:annotation-driven'。

    ------Configuration File----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-driven/>
<tx:annotation-driven/>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />     
    <property name="configLocation">
        <value>/WEB-INF/classes/hibernate.cfg.xml</value>               
    </property>
</bean>
<bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
</bean>
...

另外,我添加了两个注释驱动,因为事务不起作用,tx:annotation-driven是否使用了来自hibernate的自己会话的transactionManager

我使用hibernate3使用派生的sessionFactory,因此在方法运行更新查询之前只需要一个事务。

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试添加<?xml version="1.0" encoding="UTF-8"?>并更好地使用基于java的配置