posts - 23, comments - 0, trackbacks - 0, articles - 3
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

spring事务管理配置总结

Posted on 2008-08-18 17:05 beauty9235 阅读(900) 评论(0)  编辑  收藏

作者: beauty9235  链接:http://beauty9235.javaeye.com/blog/229654  发表时间: 2008年05月23日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

事务代理标准写法

 

<!--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>

本文的讨论也很精彩,浏览讨论>>


JavaEye推荐




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


网站导航: