简体   繁体   English

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

[英]Spring Transaction configuration programmatically with Spring 4.3.4 version

We are currently migrating from xml configuration to complete annotation with java configuration based Spring application.我们目前正在从 xml 配置迁移到基于 java 配置的 Spring 应用程序的完整注释。 With the annotation approach @Transactional we can achieve but the we need to write for each and every method.使用注释方法@Transactional我们可以实现,但我们需要为每个方法编写。

In XML we configured (OLD).我们在 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>

transactionManager is org.springframework.orm.hibernate3.HibernateTransactionManager事务管理器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 is parent class of each service class. txProxyTemplate是每个服务类的父类。

So, please suggest how to configure similar code in java configuration.所以,请建议如何在java配置中配置类似的代码。 Thanks for your valuable time spent and support us.感谢您花费宝贵的时间和支持我们。

@Barath Comment @Barath 评论

Bean豆角,扁豆

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

How to configure with each service implementation class, we can use it for single as a service layer may contain N classes.如何配置每个服务实现类,我们可以单独使用,因为一个服务层可能包含N个类。 There is a method setTarget(Object target).有一个方法 setTarget(Object target)。 Now how can we configure all the N classes.现在我们如何配置所有的 N 个类。 Please sugggest how can we configure.请建议我们如何配置。

A sample configuration for this case :这种情况下的示例配置:

    @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