随笔 - 37  文章 - 14  trackbacks - 0
<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿

随笔分类

随笔档案

文章分类

相关链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜

二、声明式事务管理
        Spring声明式管理主要是通过TransactionProxyFactoryBean来包装要管理的目标对象,代理目标对象要实现的接口。
看如下代码
首先定义一个目标对象

    <bean id="spcJlxDataSaveServiceTarget"
        class
="com.spc.spring.business.service.SpcJlxDataSaveServiceImp"
        abstract
="false" singleton="true" lazy-init="default"
        autowire
="default" dependency-check="default">
        
<property name="spcGzxmServiceImp">
            
<ref bean="spcGzxmServiceImp" />
        
</property>
        
<property name="spcJlxDataService">
            
<ref bean="spcJlxDataService" />
        
</property>
    
</bean>

同编程式事务管理,都需要有transactionManager的对象,这里就不再重复设置;下面看如果设置TransactionProxyFactoryBean来代理目标对象

     <bean id="spcJlxDataSaveServiceImp"
     class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
     
<property name="proxyInterfaces">
         
<list>
             
<value>com.spc.spring.business.ISpcJlxDataSaveService</value>
         
</list>
     
</property>
     
<property name="target">
         
<ref bean="spcJlxDataSaveServiceTarget"/>
     
</property>
     
<property name="transactionManager">
         
<ref bean="transactionManager"/>
     
</property>
     
<property name="transactionAttributes">
         
<props>
         
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
         
</props>
     
</property>
     
</bean>

target属性是目标对象;proxyInterface是目标对象所实现的接口;transactionAttributes属性是定义事务的设置级别等。其中"-Exception"表示对所有的异常都做回滚动作。
TransactionTemplatate
如果要给对象添加更多的事务属性,那么使用上述配置便会变得非常臃肿。为了避免该问题你可以使用Transaction Template Beanr为了继承这个bean
的所有bean指定属性如下,1:先定义一个tmplate bean

<bean id="txProxyTemplate" abstract="true"
class
="org.springframework.transaction.interceptor.
TransactionProxyFactoryBean"
>
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
2:创建一个应用此模板的bean时,使用parent属性来引用此模板的id。然后在target属性中将这个类定义为一个内部bean
<bean id="userManager" parent="txProxyTemplate">
<property name="target">
<bean class="org.appfuse.service.impl.UserManagerImpl">
<property name="userDAO"><ref bean="userDAO"/></property>
</bean>
</property>
</bean>
TransactionAttributeSource
另一种配置声明式事务的是指定一个引用了NameMatchTransactionAttributeSource类并定义了方法和它们行为的bean。这种策略不及template bean简洁,因为它要求用TransactionProxyFactoryBean来包装所有的bean。
1:创建一个bean来描述事务属性
<bean name="txAttributes"
class
="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource"
>
<property name="properties">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

2:创建一个包装事务的bean定义,在transactionAttributeSource属性中引用这个bean

<bean id="userManager"
class
="org.springframework.transaction.interceptor.
TransactionProxyFactoryBean"
>
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<bean class="org.appfuse.service.impl.UserManagerImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
</property>
<property name="transactionAttributeSource">
<ref bean="txAttributes"/>
</property>
</bean>
posted on 2007-08-09 14:46 扭曲的铅笔 阅读(247) 评论(0)  编辑  收藏 所属分类: Spring

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


网站导航: