今天在做東西的時候遇到了個問題,我寫的代碼一般是一個manager對應一個dao,如果在spring 中應用事務的時候 ,
一般人都用的是聲明式事務的方式,也就是用的是TransactionProxyFactoryBean這個,但是用這個后它只能針對一個target來進行 事務管理,
所以我用了自動代理方式進行事務的bean create。假如有多個方法相識的manager時就可以用這個了。

spring配置文件如下
 

<bean id="matchNameInterceptor"
          
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">  //通過名稱來進行匹配,以及transactiondefinition 
        <property name="properties">
            
<props>
                
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                
<prop key="insert*">PROPAGATION_REQUIRED</prop>
            
</props>
        
</property>
    
</bean>
    
<bean id="transactionInterceptor"
          
class="org.springframework.transaction.interceptor.TransactionInterceptor">  //事務的 interceptor 
        <property name="transactionManager" ref="transactionManager"/>
        
<property name="transactionAttributeSource" ref="matchNameInterceptor"/>
    
</bean>

    
    
<bean id="autoProxyCreator"
          
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  //最關鍵的~~自動對所列表的bean進行裝配,
        <property name="interceptorNames">
            
<list>
                
<idref local="transactionInterceptor"/>    //將interceptor 和bean聯合起來 所需要裝配的interceptor ,也可以是其他的interceptor 
            </list>
        
</property>
        
<property name="beanNames">
            
<list>
                
<idref local="iThreadManager"/>  //這里為需要裝配的bean 的list 
                <idref local="iUserManager"/>
            
</list>
        
</property>
    
</bean>



有不對的地方~歡迎各位大大指教