您的位置:首页 > 编程语言 > Java开发

spring事务_注解

2016-07-21 14:25 429 查看
在spring配置文件中引入<tx:>命名空间:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-lazy-init="true">
<!-- 开启基于注解的配置 -->

<context:annotation-config/>
<!-- 基于注解的组件扫描 -->

<context:component-scan base-package="com.surekam"/>
<!-- 基于注解的事务支持 -->

<tx:annotation-driven  transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 针对一个单独的Hibernate SessionFactory的事务管理 -->

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<!--<property name="defaultTimeout"/>-->
<!--<property name="rollbackOnCommitFailure"/>-->
</bean>
<!-- 事务、安全-拦截基础类 -->

<bean id="baseProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<!--property name="proxyTargetClass" value="true"/-->
<property name="transactionAttributeSource" ref="namingTransactionAttribute" />
</bean>
<!-- 定义拦截方法名称 -->

<bean id="namingTransactionAttribute" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop>
<prop key="create*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop>
<prop key="new*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop>
<prop key="deploy*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-com.surekam.exception.RollbackRequiredException</prop>
</props>
</property>
</bean>
service里方法:
REQUIRED:业务方法需要在一个容器里运行。如果方法运行时,已经处在一个事务中,那么加入到这个事务,否则自己新建一个新的事务。

@Transactional(propagation=Propagation.REQUIRED)
public List findTsDf(){
String hql = "from TuoshouDianfei d where 1=1";
return dao.find(hql);
}
参考:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  空间 spring 配置文件