博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring课程 Spring入门篇 5-4 advice应用(上)
阅读量:4703 次
发布时间:2019-06-10

本文共 4264 字,大约阅读时间需要 14 分钟。

1    解析

1.1  通知执行顺序

2    代码演练

 

 

 

 

1    解析

1.1  通知执行顺序

 

aop执行方式为:前置通知==>所要增强的方法==>后置通知==>最终通知

 

在出现异常时会进行:前置通知==>所要增强的方法==>异常通知==>最终通知

而用xml进行配置时,是按照我们写好的顺序进行动态组合完成,最终和后置通知是随着xml配置的前后顺序改变的,但是经过测试不会影响前置和所要增强的方法的顺序,但是会影响最终和后置通知的位置.

我认为利用环绕通知进行方法的增强(aop:around)是一个比较好的方式,不会出现顺序问题.

 

 

2    代码演练

2.1  前置通知和后置通知

业务类:

package com.imooc.aop.schema.advice.biz;public class AspectBiz {        public void biz(){        System.out.println("MoocAspect biz");    }        public void biz2(){        System.out.println("MoocAspect biz2");    }}

 

配置文件:

 

通知(名词)所在的类:

package com.imooc.aop.schema.advice;public class MoocAspect {        public void before(){        System.out.println("before");    }        public void after(){        System.out.println("after");    }}

 

测试类:

package com.imooc.test.aop;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.aop.schema.advice.biz.AspectBiz;import com.imooc.test.base.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)public class TestAOPSchemaAdvice extends UnitTestBase {        public TestAOPSchemaAdvice(){        super("classpath:spring-aop-schema-advice.xml");    }        @Test    public void testBiz(){        AspectBiz aBiz = super.getbean("AspectBiz");        aBiz.biz();        aBiz.biz2();    }}

 

打印结果:

四月 15, 2019 8:19:44 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@285855bd: startup date [Mon Apr 15 20:19:44 CST 2019]; root of context hierarchy四月 15, 2019 8:19:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]beforeMoocAspect bizafterbeforeMoocAspect biz2after四月 15, 2019 8:19:45 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@285855bd: startup date [Mon Apr 15 20:19:44 CST 2019]; root of context hierarchy

 2.2  异常通知和最终通知

业务类:

package com.imooc.aop.schema.advice.biz;public class AspectBiz {        public void biz(){        System.out.println("MoocAspect biz");        throw new RuntimeException();    }}

 

配置文件:

 

 

通知所在类:

package com.imooc.aop.schema.advice;public class MoocAspect {        public void before(){        System.out.println("before");    }        public void afterreturning(){        System.out.println("after returning");    }    public void throwing(){        System.out.println("throw");    }        public void after(){        System.out.println("Say Love me");    }}

 

测试类:

package com.imooc.test.aop;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.aop.schema.advice.biz.AspectBiz;import com.imooc.test.base.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)public class TestAOPSchemaAdvice extends UnitTestBase {        public TestAOPSchemaAdvice(){        super("classpath:spring-aop-schema-advice.xml");    }        @Test    public void testBiz(){        AspectBiz aBiz = super.getbean("AspectBiz");        aBiz.biz();    }}

 

打印日志:

 

四月 16, 2019 7:18:45 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b19b8ae: startup date [Tue Apr 16 07:18:45 CST 2019]; root of context hierarchy四月 16, 2019 7:18:45 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]beforeMoocAspect bizthrowSay Love me四月 16, 2019 7:18:46 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4b19b8ae: startup date [Tue Apr 16 07:18:45 CST 2019]; root of context hierarchy

 

转载于:https://www.cnblogs.com/1446358788-qq/p/10714660.html

你可能感兴趣的文章
项目开发总结报告(GB8567——88)
查看>>
SSH加固
查看>>
端口扫描base
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
python 二维字典
查看>>
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>
记一些从数学和程序设计中体会到的思想
查看>>
题目1462:两船载物问题
查看>>
POJ 2378 Tree Cutting(树形DP,水)
查看>>
UVA 116 Unidirectional TSP (白书dp)
查看>>
第三方测速工具
查看>>
数据访问 投票习题
查看>>