事务代理标准写法

<!--DAO层接口实现-->

<beanid="userDAO"class="net.test.dao.UserDAO">

<propertyname="sessionFactory">

<reflocal="sessionFactory"/>

</property>

</bean>

<!--业务层接口实现,把DAO注入到Service里面-->

<beanname="userServiceTarget"class="net.test.service.UserService">

<propertyname="userDAO">

<refbean="userDAO"/>

</property>

</bean>

<!--spring代理业务层的事务管理-->

<beanid="userServiceProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<propertyname="transactionManager">

<reflocal="transactionManager"/>

</property>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_REQUIRED,readOnly</prop>

</props>

</property>

<propertyname="target">

<refbean="userServiceTarget"/>

</property>

</bean>

事务代理简写法

<beanid="baseTxProxy"lazy-init="true"

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<propertyname="transactionManager">

<refbean="transactionManager"/>

</property>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_REQUIRED,readOnly</prop>

</props>

</property>

</bean>

<beanid="userDAO"class="net.test.dao.UserDAO">

<propertyname="sessionFactory">

<reflocal="sessionFactory"/>

</property>

</bean>

<beanid="userServiceProxy"parent="baseTxProxy">

<propertyname="target">

<beanclass="net.test.service.UserService">

<propertyname="userDAO">

<reflocal="userDAO"/>

</property>

</bean>

</property>

</bean>

事务自动化代理写法

<!--定义事务拦截器bean-->

<beanid="transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor">

<propertyname="transactionManager"ref="transactionManager"/>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="update*">PROPAGATION_REQUIRED</prop>

<propkey="delete*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_REQUIRED,readOnly</prop>

<propkey="query*">PROPAGATION_REQUIRED,readOnly</prop>

<propkey="*">PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>

<!--定义BeanNameAutoProxyCreator-->

<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<propertyname="beanNames">

<!--

所有名字以DAO,Service结尾的bean,

将由该"bean后处理器"为其创建事务代理;

实际上应该在业务层进行事务管理,

这里只是举一个简单例子

-->

<value>*DAO,*ServiceProxy</value>

</property>

<!--下面定义BeanNameAutoProxyCreator所需的事务拦截器-->

<propertyname="interceptorNames">

<list>

<!--可以增加其他的拦截器-->

<value>transactionInterceptor</value>

</list>

</property>

</bean>

<beanid="userDAO"class="net.test.dao.UserDAO">

<propertyname="sessionFactory">

<reflocal="sessionFactory"/>

</property>

</bean>

<beanid="userServiceProxy"class="net.test.service.UserService">

<propertyname="userDAO">

<refbean="userDAO"/>

</property>

</bean>

转自:http://www.blogjava.net/beauty9235/archive/2008/08/18/222841.html