【问题标题】:Spring Boot aop tx advice in java config without xml config没有xml配置的java配置中的Spring Boot aop tx建议
【发布时间】:2018-07-05 03:39:38
【问题描述】:

我有带有 Atomikos 和 JOOQ 的 Spring Boot 应用程序(具有多个数据源 db1 和 db2) 我有如下 XML 配置,我想将其转换为 java 配置。

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

我已经看到一些 stackoverflow 问题已经在问这个问题,但是它们很旧并且没有找到任何解决方案。

JavaConfig: Replacing aop:advisor and tx:advice

所以想知道提供的 XML 配置的确切 java 配置。

【问题讨论】:

  • stackoverflow.com/a/38725266/1128953 这个答案已经回答了这个问题。如果您只想添加事务,@Transactional 注释是一个不错的选择。
  • 我可以添加 AOP,但是当我的方法名称以 processnew 开头时,如何添加启动新事务。

标签: spring spring-boot spring-aop spring-transactions spring-java-config


【解决方案1】:

我得到了几乎接近上述 xml 配置的解决方案。 在下面的解决方案中,我将 aop 应用于 bean 名称而不是包内的类。

@Bean(name = "transactionInterceptor")
public TransactionInterceptor transactionInterceptor(PlatformTransactionManager platformTransactionManager) {
    TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
    transactionInterceptor.setTransactionManager(platformTransactionManager);
    Properties transactionAttributes = new Properties();
    transactionAttributes.setProperty("*", "PROPAGATION_REQUIRED,-Throwable");
    transactionAttributes.setProperty("tranNew*", "PROPAGATION_REQUIRES_NEW,-Throwable");
    transactionInterceptor.setTransactionAttributes(transactionAttributes);
    return transactionInterceptor;
}

@Bean
public BeanNameAutoProxyCreator transactionAutoProxy() {
    BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();
    transactionAutoProxy.setProxyTargetClass(true);
    transactionAutoProxy.setBeanNames("*Service");
    transactionAutoProxy.setInterceptorNames("transactionInterceptor");
    return transactionAutoProxy;
}

【讨论】:

    猜你喜欢
    • 2011-11-26
    • 2014-07-30
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 2023-03-21
    • 2015-11-19
    相关资源
    最近更新 更多