栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

自定义Spring AOP左右+ @Transactional

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义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绑定的,但是我认为这并不重要



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509039.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号