簡體   English   中英

使用 Spring 4.3.4 版本以編程方式配置 Spring Transaction

[英]Spring Transaction configuration programmatically with Spring 4.3.4 version

我們目前正在從 xml 配置遷移到基於 java 配置的 Spring 應用程序的完整注釋。 使用注釋方法@Transactional我們可以實現,但我們需要為每個方法編寫。

我們在 XML 中配置(舊)。

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

事務管理器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 個類。 請建議我們如何配置。

這種情況下的示例配置:

    @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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM