spring + hibernate 配置事务

spring + hibernate 配置事务
上次折腾了半天,终于把延时加载配置好了。可是不配置事务总是觉得怪怪的。so..决定把事务也配置好。虽然是个小项目吧^_^.
<!--  事务配置  -->
     
<!--  事务管理器 用于hibernate的事务管理器 -->
      
< bean  id ="transactionManager"  class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory" />
        
</ property >
    
</ bean >
    
    
<!--  事务拦截器 用于对拦截的方法开启事务,其中指定了一些只读事务 -->
    
< bean  id ="transactionInterceptor"  class ="org.springframework.transaction.interceptor.TransactionInterceptor" >
        
< property  name ="transactionManager"  ref ="transactionManager" />
        
< property  name ="transactionAttributes" >
            
< props >
                
< prop  key ="*" > PROPAGATION_REQUIRED,-Exception </ prop >
                
< prop  key ="list*" > PROPAGATION_REQUIRED,readOnly </ prop >
                
< prop  key ="*list" > PROPAGATION_REQUIRED,readOnly </ prop >
                
< prop  key ="display*" > PROPAGATION_REQUIRED,readOnly </ prop >
                
< prop  key ="*display" > PROPAGATION_REQUIRED,readOnly </ prop >
                
< prop  key ="*view" > PROPAGATION_REQUIRED,readOnly </ prop >
                
< prop  key ="find*" > PROPAGATION_REQUIRED,readOnly </ prop >
                
< prop  key ="main*" > PROPAGATION_REQUIRED,readOnly </ prop >
            
</ props >
        
</ property >
    
</ bean >
    
    
<!--  自动代理,配置使所有service层bean使用事务拦截器  -->
    
< bean  class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
        
< property  name ="beanNames" >
            
< list >
            
< value > *Service </ value >
            
</ list >
        
</ property >
        
< property  name ="interceptorNames" >
            
< list >
                
< value > transactionInterceptor </ value >
            
</ list >
        
</ property >
    
</ bean >

    
< bean  class ="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor" >
        
< property  name ="transactionInterceptor"  ref ="transactionInterceptor" />
    
</ bean >
    
<!--  事务配置结束  -->

简单说明一下,其中是用了spring提供的 BeanNameAutoProxyCreator这个自动代理服务,自动对名为XXXService的的bean使用使用拦截器开启事务,而在 transactionInterceptor 则定义了事务的属性,限定了一些只读的事务以提搞效率。

你可能感兴趣的:(spring + hibernate 配置事务)