黑马程序员:带你玩转Spring中多个AOP的执行流程
黑马程序员郑州中心编著
黑马程序员:带你玩转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字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [实用文档]李践-有效提升销售的12大黄金法则8-大
- [实用文档]党支部换届工作方案
- [实用文档]2013年下期电子商务专业部宣传工作计划
- [实用文档]方庄一矿通风、钻探绩效工资考核管理办
- [实用文档]项目一 认识企业物流认识企业物流
- [实用文档]MBI_Display_产品蓝图规画
- [实用文档]北京市建筑业劳务作业人员普法维权培训
- [实用文档]锅炉燃烧调整与运行优化
- [实用文档]4支付结算业务的核算
- [实用文档]米什金_货币金融学_第9版各章学习指导
- [实用文档]水泥混凝土路面硬化工程施工组织设计
- [实用文档]钢筋工程安全技术交底书
- [实用文档]关于公布华中师范大学本科毕业论文
- [实用文档]太原市园林绿化施工合同范本 2
- [实用文档]周日辅导 初中英语分类复习单项选择题(
- [实用文档]第四章 文化经纪人的管理形式 第二节
- [实用文档]学宪法讲宪法竞赛题库
- [实用文档]《数值计算方法》期末考试模拟试题二
- [实用文档]爱词霸学英语:每日一句( 十月)
- [实用文档]2014年国家公务员面试:无领导小组讨论
- 新课程主要理念和教学案例分析汇编(24
- 英国人的快乐源于幸福的家庭生活
- 七年级上册第一次月考模拟数学试卷
- 真丝及仿真丝的种类有哪些?
- 【最新】华师大版八年级数学下册第十六
- 高中英语3500个必背单词
- 我可以接受失败,但我不能接受放弃!
- 最近更新沪科版八年级物理上册期末试卷
- 绿化工作先进乡镇事迹材料
- 鲁教版九年级上册思想品德教学计划
- 英语音标的分类
- 地下室底板无梁楼盖与普通梁板结构形式
- 美容师黄金销售话术
- 雅思写作满分作文备考方法
- 血清甲状腺激素测定与高频彩色多普勒超
- 1度浅析装修对室内空气品质的影响
- 2017-2022年中国汞矿行业深度分析与投
- 计算机二级VB公共基础知识
- (何勇)秸秆禁烧_重在寻找出路
- 内外墙抹灰工程分包施工合同1




