Spring通过AOP实现声明式事务管理
通常通过TransactionProxyFactoryBean设置Spring事务代理
我们需要一个目标对象包装在事务代理中
这个目标对象一般是一个普通Java对象的bean
当我们定义TransactionProxyFactoryBean时
必须提供一个相关的 PlatformTransactionManager的引用和事务属性
事务属性含有上面描述的事务定义
PlatformTransactionManager
HibernateTransactionManager需要一个SessionFactory的引用
JtaTransactionManager
一.把事务放置在了DAO层
<!—hibernateTransactionManager>
<bean id=transactionManager
class=orgspringframeworkormhibernateHibernateTransactionManager>
<property name=sessionFactory>
<ref local=sessionFactory />
</property>
</bean>
<!—DAO层接口实现>
<bean id=companyDAOTarget
class=comvstsoftquerycompanydaoimplCompanyDAOImpl>
<property name=sessionFactory>
<ref local=sessionFactory />
</property>
</bean>
<!—spring对DAO层的事务代理>
<bean id=companyDAOProxy
class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>
<property name=transactionManager>
<ref bean=transactionManager />
</property>
<property name=target>
<ref local=companyDAOTarget />
</property>
<property name=transactionAttributes>
<props>
<prop key=insert*>PROPAGATION_REQUIRED</prop>
<prop key=delete*>PROPAGATION_REQUIRED</prop>
<prop key=find*>
PROPAGATION_REQUIREDreadOnly
</prop>
</props>
</property>
</bean>
<!—业务层接口实现把DAO注入到Service里面>
<bean name=companyManageTarget
class=comvstsoftquerycompanyserviceimplCompanyManageTarget>
<property name=companyDAO>
<ref bean=companyDAOProxy />
</property>
</bean>
<!—spring对Service层的代理>
<bean id=companyManageProxy
class=orgspringframeworkaopframeworkProxyFactoryBean>
<property name=proxyInterfaces>
<value>comvstsoftquerycompanyserviceCompanyManage</value>
</property>
<property name=target>
<ref bean=companyManageTarget />
</property>
</bean>
<!—配置struts访问把service层注入到action里面>
<bean name=/company
class=comvstsoftquerycompanywebactionCompanyAction singleton=false>
<property name=companyManage>
<ref local=companyManageProxy />
</property>
</bean>
二.把事务放置在了Service层
<!—jtaTransactionManager>
<bean id=jtaTransactionManager
class=orgspringframeworktransactionjtaJtaTransactionManager />
<!—DAO层接口实现>
<bean id=companyDAOTarget
class=comvstsoftquerycompanydaoimplCompanyDAOImpl>
<property name=sessionFactory>
<ref local=sessionFactory />
</property>
</bean>
<!—spring对DAO层的代理>
<bean id=companyDAOProxy
class=orgspringframeworkaopframeworkProxyFactoryBean>
<property name=proxyInterfaces>
<value>comvstsoftquerycompanydaoCompanyDAO</value>
</property>
<property name=target>
<ref bean=companyDAOTarget />
</property>
</bean>
<!—业务层接口实现把DAO注入到Service里面>
<bean name=companyManageTarget
class=comvstsoftquerycompanyserviceimplCompanyManageTarget>
<property name=companyDAO>
<ref bean=companyDAOProxy />
</property>
</bean>
<!—spring代理业务层的事务管理>
<bean id=companyManageProxy
class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>
<property name=transactionManager>
<ref local=jtaTransactionManager />
</property>
<property name=transactionAttributes>
<props>
<prop key=set*>PROPAGATION_REQUIRED</prop>
<prop key=find*>PROPAGATION_REQUIREDreadOnly</prop>
</props>
</property>
<property name=target>
<ref bean=companyManageTarget />
</property>
</bean>
<!—配置struts访问把service层注入到action里面>
<bean name=/company
class=comvstsoftquerycompanywebactionCompanyAction singleton=false>
<property name=companyManage>
<ref local=companyManageProxy />
</property>
</bean>
在service层的接口实现CompanyManageImpl里面有个setData方法按顺序执行数据查询数据删除数据插入数据库行为如果哪一步出异常(运行时异常)事务回滚只有所有行为都没成功事务提交