【问题标题】:Spring Transaction configuration programmatically with Spring 4.3.4 version使用 Spring 4.3.4 版本以编程方式配置 Spring Transaction
【发布时间】:2017-05-31 04:00:05
【问题描述】:

我们目前正在从 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>

transactionManagerorg.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 将xml转换为具有属性的各个类的java配置

标签: spring hibernate spring-mvc spring-transactions


【解决方案1】:

此案例的示例配置:

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

【讨论】:

  • 我理解这很复杂。当您从 xml 迁移到 java 配置时,最好继续使用 @Transcational 本身
  • @Bean TestEntity testEntity(){ return new TestEntity(); }
  • 它只是一个示例。您可以根据您的配置设置目标
  • 这里是我们可以配置的每个类,但是我们如何在不使用@Transactional 的情况下为所有服务层类配置。
  • 如何使用 AOP 应用切面?让我搜索任何示例并回复您
猜你喜欢
  • 2015-02-04
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多