自定义Spring AOP左右+ @Transactional

自定义Spring AOP左右+ @Transactional,第1张

自定义Spring AOP左右+ @Transactional

如果未为@Transactional注释配置order属性,则
负责事务属性的Advisor -
AbstractPointcutAdvisor
(实际上是它的子类之一)将返回Ordered
.LOWEST_PRECEDENCE,它被定义为Integer.MAX_VALUE。

负责自定义AOP通知的Advisor是同一AbstractPointcutAdvisor的子类,它将查看实际的Advice是否
实现Ordered接口,如果是,则在排序过程中将使用提供的值。如果自定义AOP通知未实现Ordered接口,则Advisor将返回相同的默认Ordered.LOWEST_PRECEDENCE,并且排序结果会变得
有些 不可预测。

因此,为@Transactional注释配置order属性,例如这样

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:util="http://www.springframework.org/schema/util"    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd>.......<tx:annotation-driven transaction-manager="transactionManager" proxy-target- order="[Order for @Transactional]"><beans/>

您的自定义AOP建议实施如下所示

import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.core.Ordered;@Aspectpublic class CustomAspect implements Ordered {    @Around(value = "@annotation(CustomAnnotation)")    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {    ...     }    ....    @Override    public int getOrder() {         return [Order for @CustomAnnotation];    }    ....}

那么您在整个应用程序中的排序就可能拥有所有的自由(但还是静态的)。

在后台,是AspectJAwareAdvisorAutoProxyCreator在代理初始化时使用比较器org.springframework.aop.aspectj.autoproxy.AspectJPrecedenceComparator将Advisor排序,该排序器将排序委托给OrderComparator。后来,在实际执行后,AopProxy的具体实现为特定方法保存了一组建议,将其称为拦截器,并且我认为这可以用于动态重新排序,但是对我而言,这些东西似乎都不容易可访问和/或可配置的。

我的环境是Spring Beans,TX,AOP-所有版本4.0.3。我也有两个自定义的事务管理器,一个是Hibernate绑定的,一个是JDBC
DataSource绑定的,但是我认为这并不重要



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5501430.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存