Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot aop tx advice in java config without xml config

I have Spring boot application with Atomikos and JOOQ (with multiple data sources db1 & db2) I have XML configuration like below and I want to convert it to java config.

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <!-- All methods use the default transaction settings (see below) -->
        <tx:method name="processNew*" propagation="REQUIRES_NEW"/>
        <tx:method name="onMessage*" propagation="SUPPORTS"/>
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>

I have seen some stackoverflow question already asking this question, but they're old and haven't found any solution.

JavaConfig: Replacing aop:advisor and tx:advice

So want to know exact java configuration for provided XML configuration.

like image 593
D7thename Avatar asked May 16 '26 08:05

D7thename


1 Answers

maybe later but solution in below works for me.

https://linuxtut.com/en/fc0e4145c5249c7f5f80/

    @Bean
    public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
            PlatformTransactionManager transactionManager) {
        RuleBasedTransactionAttribute attribute = new RuleBasedTransactionAttribute();
        attribute.setTimeout(30);
        attribute.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        source.addTransactionalMethod("*", attribute);

        BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor(
);
        advisor.setTransactionAttributeSource(source);
        advisor.setAdvice(new TransactionInterceptor(transactionManager, source));
        // Implement filter conditions and`setClassFilter`Call
        advisor.setClassFilter(clazz -> AnnotationUtils.findAnnotation(clazz, Service.class) != null);
        return advisor;
    }

like image 147
zhangc Avatar answered May 19 '26 03:05

zhangc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!