事务代理标准写法 <!--DAO层接口实现 --><bean id="userDAO" class="net.test.dao.UserDAO"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property></bean> <!--业务层接口实现,把DAO注入到Service里面 --> <bean name="userServiceTarget" class="net.test.service.UserService"> <property name="userDAO"> <ref bean="userDAO" /> </property></bean><!--spring代理业务层的事务管理 --><bean id="userServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> <property name="target"> <ref bean="userServiceTarget" /> </property></bean> 事务代理简写法 <bean id="baseTxProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="userDAO" class="net.test.dao.UserDAO"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="userServiceProxy" parent="baseTxProxy"> <property name="target"> <bean class="net.test.service.UserService"> <property name="userDAO"> <ref local="userDAO" /> </property> </bean> </property> </bean> 事务自动化代理写法 <!-- 定义事务拦截器bean --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!--定义BeanNameAutoProxyCreator--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <!-- 所有名字以DAO,Service结尾的bean, 将由该"bean后处理器"为其创建事务代理; 实际上应该在业务层进行事务管理, 这里只是举一个简单例子 --> <value>*DAO,*ServiceProxy</value> </property> <!--下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames"> <list> <!--可以增加其他的拦截器--> <value>transactionInterceptor</value> </list> </property> </bean> <bean id="userDAO" class="net.test.dao.UserDAO"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="userServiceProxy" class="net.test.service.UserService"> <property name="userDAO"> <ref bean="userDAO" /> </property> </bean>