spring事务管理配置总结

事务代理标准写法

 

<!--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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值