2017-01-16 66 views
0

我們目前正在從xml配置遷移到基於Java配置的Spring應用程序的完整註釋。使用註釋方法@Transactional我們可以實現但我們需要爲每一種方法編寫。使用Spring 4.3.4版本以編程方式Spring Transaction配置

在XML中我們配置了(OLD)。

<bean id="txProxyTemplate" abstract="true" 
     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
     <property name="transactionManager"> 
      <ref bean="transactionManager" /> 
     </property> 
     <property name="transactionAttributes"> 
      <props> 
       <prop key="delete*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop> 
       <prop key="update*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop> 
       <prop key="save*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop> 
       <prop key="get*">PROPAGATION_SUPPORTS,ISOLATION_READ_COMMITTED,readOnly</prop> 
       <prop key="is*">PROPAGATION_SUPPORTS,ISOLATION_READ_COMMITTED,readOnly</prop> 
       <!--<prop key="*">PROPAGATION_REQUIRED</prop> --> 
      </props> 
     </property> 
    </bean> 

transactionManager的org.springframework.orm.hibernate3.HibernateTransactionManager

<bean id="xxxxSVC" parent="txProxyTemplate"> 
     <property name="target"> 
      <bean class="XXX.XXX.XXX.SVCImpl"> 
       <property name="xxxxDao" ref="xxxDao"></property> 

      </bean> 
     </property> 
    </bean> 

txProxyTemplate是父類的每個服務類別的。

因此,請建議如何在java配置中配置類似的代碼。感謝您花費寶貴的時間和支持我們。

@Barath評論

@Bean 
    public TransactionProxyFactoryBean setTransactionProperties() throws IOException { 
     TransactionProxyFactoryBean transactionProxyFactoryBean = new TransactionProxyFactoryBean(); 
     transactionProxyFactoryBean.setTransactionManager(transactionManager(sessionFactory())); 
     Properties transactionAttributesProps = new Properties(); 
     transactionAttributesProps.setProperty("delete*", "PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED"); 
     transactionAttributesProps.setProperty("update*", "PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED"); 
     transactionAttributesProps.setProperty("save*", "PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED"); 
     transactionAttributesProps.setProperty("get*", "PROPAGATION_SUPPORTS,ISOLATION_READ_COMMITTED,readOnly"); 
     transactionAttributesProps.setProperty("is*", "PROPAGATION_SUPPORTS,ISOLATION_READ_COMMITTED,readOnly"); 
     transactionProxyFactoryBean.setTransactionAttributes(transactionAttributesProps); 

     transactionProxyFactoryBean.afterPropertiesSet(); 
     return transactionProxyFactoryBean; 
    } 

如何與每個服務實現類配置,我們可以用它來作爲單一服務層可能包含N個類別。有一個方法setTarget(Object target)。現在我們如何配置所有的N個類。請建議我們如何配置。

+0

你可以簡單地利用@Bean以XML轉換爲與性能 – Barath

回答

0

對於這種情況下的配置實例:

@Bean 
    public TransactionProxyFactoryBean txProxyTemplate(){ 

     TransactionProxyFactoryBean txFactory=new TransactionProxyFactoryBean(); 
     txFactory.setTransactionManager(new JpaTransactionManager()); // any transcation manager 
     txFactory.setTransactionAttributes(properties()); 
     return txFactory; 

    } 

    @Bean 
    Properties properties(){ 
     Properties properties=new Properties(); 
     properties.put("delete*", "PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED"); 
     //set al lthe properties 
     return properties; 
    } 


    @Bean 
    public TransactionProxyFactoryBean xxxxSVC(TransactionProxyFactoryBean txFactory){ 
     txFactory.setTarget(testEntity()); 
     return txFactory; 
    } 

    @Bean 
    TestEntity testEntity(){ 
     return new TestEntity(); 
    } 
+0

各階級的Java配置這是複雜的我明白了。當你從xml遷移到java配置時,你最好使用@Transcational本身 – Barath

+0

@Bean TestEntity testEntity(){ return new TestEntity(); } – Nageshwar

+0

它只是一個樣本。您可以根據您的配置設置目標 – Barath

相關問題