随笔-9  评论-168  文章-266  trackbacks-0

Spring 2.0 事务处理方式: http://www.blogjava.net/algz/articles/262941.html

前者:   都需创建HibernateTransactionManager类.
<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

(一)org.springframework.transaction.interceptor.TransactionProxyFactoryBean:  创建代理事务.

事务代理Bean:

 1 <bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
 2       <property name="transactionManager">
 3             <ref bean="transactionManager" />
 4       </property>
 5       <property name="target">
 6            <ref local="userDAO" />
 7       </property>
 8       <property name="transactionAttributes">
 9            <props>
10                 <prop key="insert*">PROPAGATION_REQUIRED</prop>
11           </props>
12       </property>
13 </bean>


引用代理Bean:

1 <bean name="/login" class="com.test.struts.action.LoginAction" singleton="false">
2     <property name="userDAO">                                     <!--这里的userDAO是LoginAction的属性-->
3           <ref bean="userDAOProxy" />                              <!--ref bean=userDAO,  调用的是原来的Bean,  则不是在事务中运行-->
4     </property>
5 </bean>



(二)org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator:  根据BeanName自动创建事务代理
org.springframework.transaction.interceptor.TransactionInterceptor:  拦截器

 1<!--                    无需引用代理BEAN(bean id 可去掉),业务层(/login)可直接调用原来的Bean==>userDAO(userDAO已封装入事务)                          -->
 2 <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
 3       <property name="beanNames">
 4             <value>userDAO</value>
 5       </property>
 6       <property name="interceptorNames">
 7             <list>
 8                 <value>transactionInterceptorName</value>
 9             </list>
10        </property>
11 </bean>
12
13<!--                   拦截器,定义事务策略                               -->
14 <bean id="transactionInterceptorName" class="org.springframework.transaction.interceptor.TransactionInterceptor">
15       <property name="transactionAttributes">
16           <props>
17               <prop key="*">PROPAGATION_REQUIRED</prop>                    <!--事务策略定义-->
18           </props>
19       </property>
20       <property name="transactionManager">
21             <ref local="transactionManager" />
22       </property>
23 </bean>


 

(三)DefaultAdvisorAutoProxyCreator:  自动创建事务代理.(与二类似)
posted on 2007-11-26 17:00 紫蝶∏飛揚↗ 阅读(1102) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: