Spring 4.3.4版本

时间:2017-01-16 10:28:58

标签: spring hibernate spring-mvc spring-transactions

我们目前正在使用基于Java配置的Spring应用程序从xml配置迁移到完整注释。使用注释方法 @Transactional ,我们可以实现,但我们需要为每个方法编写。

在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个类。请建议我们如何配置。

1 个答案:

答案 0 :(得分: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();
    }