教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 实用文档 >

黑马程序员:带你玩转Spring中多个AOP的执行流程

来源:网络收集 时间:2026-04-02
导读: 黑马程序员郑州中心编著 黑马程序员:带你玩转Spring中多个AOP的执行流程 Spring声明式事务是基于AOP实现的,那么,如果我们在同一个方法自定义多个AOP,我们如何指定他们的执行顺序呢? 首先:配置AOP执行顺序的三种方式: 1.通过实现org.springframework.c

黑马程序员郑州中心编著

黑马程序员:带你玩转Spring中多个AOP的执行流程

Spring声明式事务是基于AOP实现的,那么,如果我们在同一个方法自定义多个AOP,我们如何指定他们的执行顺序呢?

首先:配置AOP执行顺序的三种方式:

1.通过实现org.springframework.core.Ordered接口

1.@Component

2.@Aspect

3.@Slf4j

4.public class MessageQueueAopAspect1 implements Ordered{@Ove

rride

5. public int getOrder() {

6. // TODO Auto-generated method stub

7.return 2;

8. }

9.

10.}

2.通过注解

1.@Component

2.@Aspect

3.@Slf4j

4.@Order(1)

5.public class MessageQueueAopAspect1{

黑马程序员郑州中心编著

6.

7. ...

8.}

3.通过配置文件配置

1.<aop:config expose-proxy="true">

2.<aop:aspect ref="aopBean"order="0">

3.<aop:pointcut id="testPointcut"expression="@annotation(xx

x.xxx.xxx.annotation.xxx)"/>

4.<aop:around pointcut-ref="testPointcut"method="doAround

"/>

5.</aop:aspect>

6.</aop:config>

我们在同一个方法上加以下两个AOP,看看究竟。

1.@Component

2.@Aspect

3.@Slf4j

4.public class MessageQueueAopAspect1 implements Ordered{

5.

6.@Resource(name="actionMessageProducer")

黑马程序员郑州中心编著

7.private IProducer<MessageQueueInfo> actionProducer;

8.

9.@Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire1)")

10.private void pointCutMethod() {

11. }

12.

13.//声明前置通知

14.@Before("pointCutMethod()")

15.public void doBefore(JoinPoint point) {

16. http://www.77cn.com.cn("MessageQueueAopAspect1:doBefore");

17.return;

18. }

19.

20.//声明后置通知

21.@AfterReturning(pointcut = "pointCutMethod()", returning = "returnVal

ue")

22.public void doAfterReturning(JoinPoint point,Object returnValue) {

23. http://www.77cn.com.cn("MessageQueueAopAspect1:doAfterReturning");

24. }

25.

26.//声明例外通知

黑马程序员郑州中心编著

27.@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")

28.public void doAfterThrowing(Exception e) {

29. http://www.77cn.com.cn("MessageQueueAopAspect1:doAfterThrowing");

30. }

31.

32.//声明最终通知

33.@After("pointCutMethod()")

34.public void doAfter() {

35. http://www.77cn.com.cn("MessageQueueAopAspect1:doAfter");

36. }

37.

38.//声明环绕通知

39.@Around("pointCutMethod()")

40.public Object doAround(ProceedingJoinPoint pjp) throws Throwable {

41. http://www.77cn.com.cn("MessageQueueAopAspect1:doAround-1");

42. Object obj = pjp.proceed();

43. http://www.77cn.com.cn("MessageQueueAopAspect1:doAround-2");

44.return obj;

45. }

46.

47.@Override

48.public int getOrder() {

黑马程序员郑州中心编著

49.return1001;

50. }

51.}

1.@Component

2.@Aspect

3.@Slf4j

4.public class MessageQueueAopAspect2 implements Ordered{

5.

6.@Resource(name="actionMessageProducer")

7.private IProducer<MessageQueueInfo> actionProducer;

8.

9.@Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire2)")

10.private void pointCutMethod() {

11. }

12.

13.

14.//声明前置通知

15.@Before("pointCutMethod()")

16.public void doBefore(JoinPoint point) {

黑马程序员郑州中心编著

17. http://www.77cn.com.cn("MessageQueueAopAspect2:doBefore");

18.return;

19. }

20.

21.//声明后置通知

22.@AfterReturning(pointcut = "pointCutMethod()", returning = "returnVal

ue")

23.public void doAfterReturning(JoinPoint point,Object returnValue) {

24. http://www.77cn.com.cn("MessageQueueAopAspect2:doAfterReturning");

25. }

26.

27.//声明例外通知

28.@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")

29.public void doAfterThrowing(Exception e) {

30. http://www.77cn.com.cn("MessageQueueAopAspect2:doAfterThrowing");

31. }

32.

33.//声明最终通知

34.@After("pointCutMethod()")

35.public void doAfter() {

36. http://www.77cn.com.cn("MessageQueueAopAspect2:doAfter");

37. }

黑马程序员郑州中心编著

38.

39.//声明环绕通知

40.@Around("pointCutMethod()")

41.public Object doAround(ProceedingJoinPoint pjp) throws Throwable {

42. http://www.77cn.com.cn("MessageQueueAopAspect2:doAround-1");

43. Object obj = pjp.proceed();

44. http://www.77cn.com.cn("MessageQueueAopAspect2:doAround-2");

45.return obj;

46. }

47.

48.@Override

49.public int getOrder() {

50.return1002;

51. }

52.}

1.@Transactional(propagation=Propagation.REQUIRES_NEW)

2.@MessageQueueRequire1

3.@MessageQueueRequire2

4.public PnrPaymentErrCode bidLoan(String id){

5. ...

黑马程序员郑州中心 编著 6.

}

看看执行结果:

从上面的测试我们看到,确实是order 越小越是最先执行,但更重要的是最先执行的最后结束。

这个不难理解,spring AOP 就是面向切面编程,什么是切面,画一个图来理解下:

由此得出:spring aop 就是一个同心圆,要执行的方法为圆心,最外层的order 最小。从最外层按照AOP1、AOP2的顺序依次执行doAround 方法,doBefore 方法。然后执行method 方法,最后按 …… 此处隐藏:2322字,全部文档内容请下载后查看。喜欢就下载吧 ……

黑马程序员:带你玩转Spring中多个AOP的执行流程.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1111326.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)