事务代理标准写法
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/db91af3672d963fad1a2d3e4.jpg)
<!--DAO层接口实现 -->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/2357b0b5fdf9ddb930add1e4.jpg)
<bean id="userDAO" class="net.test.dao.UserDAO">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/aa770ef61ef61aa27709d7e4.jpg)
<property name="sessionFactory">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/4b78c9ed47529a7badafd5e4.jpg)
<ref local="sessionFactory" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/0cdc49349ff4ff90a2cc2be4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/d67a2cdf9697ad948c1029e4.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/d9197edbba4bd0a438012fe4.jpg)
<!--业务层接口实现,把DAO注入到Service里面 -->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/f7c503eb005af072b90e2de4.jpg)
<bean name="userServiceTarget" class="net.test.service.UserService">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/43d4233d8355aaa155e723e4.jpg)
<property name="userDAO">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c2db79ee69bce79c2e2e21e4.jpg)
<ref bean="userDAO" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/2e3234d3107568759a5027e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/55fbbb3ada0b9e8fd46225e4.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/e1854dc096395478e4dd3be4.jpg)
<!--spring代理业务层的事务管理 -->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/afb78737a086365c91ef39e4.jpg)
<bean id="userServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/9f08e513d7b4a78ba6ef3fe4.jpg)
<property name="transactionManager">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/dd3acc19d64d2cb7ae5133e4.jpg)
<ref local="transactionManager" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/e7c3fff8e20a2293b58f31e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/ce5a8e56c5b521523b2935e4.jpg)
<property name="transactionAttributes">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/40fc2ff7b5a54868bd3109e4.jpg)
<props>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c6e45e626df38f928cb10de4.jpg)
<prop key="insert*">PROPAGATION_REQUIRED</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/4e7d5cddd6ea1a5e485403e4.jpg)
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/f764c911140fd86edc5401e4.jpg)
</props>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/33810b21820f8703935807e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/e88dd30bd0854dcfd0581be4.jpg)
<property name="target">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c90b9e80b4031df26d8119e4.jpg)
<ref bean="userServiceTarget" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/ab8dcebd0bda514718d81fe4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/1654820870836d73b0351de4.jpg)
</bean>
事务代理简写法
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/630dbe3cda6ee2b7838b13e4.jpg)
<bean id="baseTxProxy" lazy-init="true"
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/f85ea41cc7882c52403417e4.jpg)
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/d206ff1d306f4647314e15e4.jpg)
<property name="transactionManager">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/3be195084315f45294ca6be4.jpg)
<ref bean="transactionManager" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/4a9b271de0910085a68669e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/ef1fd3cad0dd185a7e3e6fe4.jpg)
<property name="transactionAttributes">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/dd53cb150665b644c83d6de4.jpg)
<props>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/05eb650bb266a1823ac763e4.jpg)
<prop key="insert*">PROPAGATION_REQUIRED</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/b3e872cd469c4e70f9dc61e4.jpg)
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/41129d3f8787004dbba167e4.jpg)
</props>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/8209d3023bfa1c5c738b65e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c074cf13f1d0965db8127be4.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/085e45123c4919b1c2ce79e4.jpg)
<bean id="userDAO" class="net.test.dao.UserDAO">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c3c7cafe449512b0fd037fe4.jpg)
<property name="sessionFactory">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/b91bc1ff75584308d7887de4.jpg)
<ref local="sessionFactory" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/86d690475dd30572869473e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/ac5dd63d0acf758f7c1e71e4.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/fd41a6c0f24503560ff477e4.jpg)
<bean id="userServiceProxy" parent="baseTxProxy">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/07cbd0199fafca98ac6e75e4.jpg)
<property name="target">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/742119d73e350c8950da4be4.jpg)
<bean class="net.test.service.UserService">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/d7bbdfc6c481508638db49e4.jpg)
<property name="userDAO">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/2b0f83c9ae8067b053664fe4.jpg)
<ref local="userDAO" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/5720e2fccf7e6d7c5d6008e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/430eb4ffcb3d015b08244de4.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/28b3d214927f7a55962b43e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/73f1a91a0a70054a34fa41e4.jpg)
</bean>
事务自动化代理写法
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/edfed605aaa137807a8947e4.jpg)
<!-- 定义事务拦截器bean -->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/4f2fe4cfdad24f1c0eb345e4.jpg)
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/015c9c53ace8de6943a75be4.jpg)
<property name="transactionManager" ref="transactionManager"/>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/75660d26e7fc087ec99559e4.jpg)
<property name="transactionAttributes">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/3872db316fce000eac4b5fe4.jpg)
<props>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/b240d3410410005e72f05de4.jpg)
<prop key="insert*">PROPAGATION_REQUIRED</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/d79ed311d8ab534f5baf53e4.jpg)
<prop key="update*">PROPAGATION_REQUIRED</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/09258000f7f475424afb51e4.jpg)
<prop key="delete*">PROPAGATION_REQUIRED</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/207aa60de4a02aa0aa6457e4.jpg)
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/26b536aa04845cfcca130ce4.jpg)
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/312ef9ef1a3fd20279f055e4.jpg)
<prop key="*">PROPAGATION_REQUIRED</prop>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/0225f5341a98a87c0b55a9e5.jpg)
</props>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/d5167b33bd0e6254eac4afe5.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/7080b11b529f660842a9ade5.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/9111b547f8f2536bcefca3e5.jpg)
<!--定义BeanNameAutoProxyCreator-->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/397c802472a7cf5a8b82a1e5.jpg)
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/b5291c1535d9684c4b90a7e5.jpg)
<property name="beanNames">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/b1c64b5657780c19574e00e4.jpg)
<!--
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/f057bb038bcb274e728da5e5.jpg)
所有名字以DAO,Service结尾的bean,
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/3045f401b0d6783a3812bbe5.jpg)
将由该"bean后处理器"为其创建事务代理;
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/0958ab75fc496256b151b9e5.jpg)
实际上应该在业务层进行事务管理,
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/73f6df5636154315d00906e4.jpg)
这里只是举一个简单例子
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/2c9b905ab35262939d8204e4.jpg)
-->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/43c7b119770a29548718bfe5.jpg)
<value>*DAO,*ServiceProxy</value>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/ca84fa1b4f43e737dbb4bde5.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/abdcb1dcf8d90015cdbf1ae4.jpg)
<!--下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/e657d35aaae4bdcc810a18e4.jpg)
<property name="interceptorNames">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/fccd347811ef9ba02f73b3e5.jpg)
<list>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/b66a6e83e851c895bd3e1ee4.jpg)
<!--可以增加其他的拦截器-->
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/a06148efe3285a99b21cb1e5.jpg)
<value>transactionInterceptor</value>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/fadf1bdad665607832fa1ce4.jpg)
</list>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c6ebb3375fa153c6a71e12e4.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c9c9850370d0569b267fb5e5.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/492f8089c845799efc1f10e4.jpg)
<bean id="userDAO" class="net.test.dao.UserDAO">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/db5e85d4f4244f6206088be5.jpg)
<property name="sessionFactory">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/dccbaad18d449079960a16e4.jpg)
<ref local="sessionFactory" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/5daa9c2dd253f9751e3089e5.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/7ddd2a3ac86b3869b9998fe5.jpg)
</bean>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/eddf324c51444487d0c86ae4.jpg)
<bean id="userServiceProxy" class="net.test.service.UserService">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/5fca97c8a79376cbc81768e4.jpg)
<property name="userDAO">
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/65e5eb2661c2784c4d088de5.jpg)
<ref bean="userDAO" />
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/c24cab039753bf4b1c9583e5.jpg)
</property>
![](http://hiphotos.baidu.com/everthingandnothing/pic/item/ab1da584b14cae7366096ee4.jpg)
</bean>
转自:http://www.blogjava.net/beauty9235/archive/2008/08/18/222841.html