声明式事务spring1.x方式
防止出现此异常:org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
在文件开头部分要加入:
1. 创建sessionFactory
<!--- 创建sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
2.配置事务管理器
<!--- 2.配置事务管理器 -->
<bean id="myHibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
3.声明式事务代理模板
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="myHibTransactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="do*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
4.那些类的哪些方法参与事务
<bean id="userBizTarget" class="com.zsw.biz.impl.TUserBizImpl">
<property name="userDao" ref="userDao" />
</bean>
<!--- Biz -->
<bean id="userDao" class="com.zsw.dao.impl.TUserDAOImpl" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--- 4.那些类的哪些方法参与事务 -->
<bean id="userBiz" parent="txProxyTemplate">
<property name="target" ref="userBizTarget" />
</bean>
完整文件如下:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--- 创建sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!--- 3.声明式事务代理模板 -->
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="myHibTransactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="do*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!--- Dao -->
<bean id="userBizTarget" class="com.zsw.biz.impl.TUserBizImpl">
<property name="userDao" ref="userDao" />
</bean>
<!--- Biz -->
<bean id="userDao" class="com.zsw.dao.impl.TUserDAOImpl" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--- 4.那些类的哪些方法参与事务 -->
<bean id="userBiz" parent="txProxyTemplate">
<property name="target" ref="userBizTarget" />
</bean>
</beans>