




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本文主要简单的介绍了如何基于Annotation方式使用AOP。本文测试使用的是Spring3.1 +AspectJ1.6. 一 示例代码1.IHelloWord.javapackage com.SpringAOP.HelloWord;public interface IHelloWord public void sayHello(String message);2.HelloWord.javapackage com.SpringAOP.HelloWord;public class HelloWord implements IHelloWord public void sayHello(String message) System.out.println(message); /int i=10/0; 3.HelloWordAspect.javapackage com.SpringAOP.HelloWord;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;Aspectpublic class HelloWordAspect Before(value=execution(* com.SpringAOP.HelloWord.HelloWord.*(.) public void beforeSayHello(JoinPoint joinPoint) System.out.println(Before :+joinPoint.getArgs()0); After(value=execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(.) & args(message) public void afterSayHello(String message) System.out.println(After : +message); Around(value=execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(.) public void aroundSayHello(ProceedingJoinPoint joinPoint) throws Throwable System.out.println(Around Before ! ); joinPceed(); System.out.println(Around After ! ); AfterThrowing(value=execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(.),throwing=ex) public void afterThrowingSayHello(Exception ex) System.out.println(After Throwing : +ex.getMessage(); AfterReturning(value=execution(public void com.SpringAOP.HelloWord.HelloWord.sayHello(.),returning=reval) public void afterReturningSayHello(String reval) System.out.println(After Returning : +reval); 4.HelloWord.xml 5.HelloWordTest.javapackage com.SpringAOP.HelloWord;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWordTest Test public void test() ApplicationContext ctx =new ClassPathXmlApplicationContext(com/SpringAOP/HelloWord/HelloWord.xml); IHelloWord helloWord = (IHelloWord)ctx.getBean(helloWord); helloWord.sayHello(Hello Word!); 6.需要导入的包7.运行结果二 说明1.pointcut语法execution for matching method execution join pointswithin 指定连接点所在的Java类型。this bean reference (Spring AOP proxy) is an instance of the given type target the target object (application object being proxied) is an instance of the given typeargs 指定传入到连接点的参数target the class of the executing object has an annotation of the given typeargs the runtime type of the actual arguments passed have annotations of the given type(s)within limits matching to join points within types that have the given annotationannotation the subject of the join point (method being executed in Spring AOP) has the given annotation组合pointcut表达式可以使用 & 、| 、! 组合pointcut表达式。execution格式execution(可见性(可选) 使用public、protected、private指定可见性。也可以为*,表示任意可见性返回值类型(必须) 指定返回值类型声明类型(可选) java包方法名(参数模式)(必须) 方法名称,和方法接收的参数异常模式(可选) 指定方法签名中是否存在异常类型)在pointcut表达式中可以使用 * 、. 、 +等通配符。* :表示若干字符(排除 . 在外). :表示若干字符(包括 . 在内)+ :表示子类 ,比如Info+ 表示Info类及其子类2.定义Adviceadvice在关联的pointcut表达式的方法运行前(before)、运行后(after)、运行前后(around)执行。Before Advice:使用Before定义Before Advice。参数:value :绑定到Advice的Pointcut表达式After returning advice在匹配的方法执行之后运行该Adivce。使用AfterReturning进行注释。参数:value、pointcut:绑定到Advice的Pointcut表达式returning:String类型,将方法的返回值绑定到Advice的参数名上。After throwing advice在pointcut匹配的方法抛出异常后,执行pointcut关联的Advice。使用AfterThrowing进行注解。参数:value、pointcut:绑定到Advice的Pointcut表达式throwing:String类型,将抛出的异常绑定到Advice的参数上。After advice使用After进行注解。参数:value :绑定到Advice的Pointcut表达式3.参数使用JoinPoint可以在Advice中的第一个参数中定义org.aspectj.lang.JoinPoint类型的参数(在around Advice中使用ProceedingJoinPoint类型的参数)java.lang.Object getArgs() returns the method argumentsSignature getSignature() returns a description of the
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025广东云浮市郁南县林业局招聘生态管护人员2人模拟试卷及答案详解(夺冠系列)
- 2025年冀北博望电力产业管理(北京)有限公司高校毕业生招聘(第三批)模拟试卷及一套完整答案详解
- 2025江苏苏州市相城城市建设投资(集团)有限公司人员招聘考前自测高频考点模拟试题及答案详解(考点梳理)
- 2025江苏南京紫金山科技产业发展集团有限公司招聘3人考前自测高频考点模拟试题及参考答案详解
- 2025广东广州市中山大学孙逸仙纪念医院耳鼻喉科医技岗位招聘1人考前自测高频考点模拟试题附答案详解(完整版)
- 2025湖南张家界市永定区发展和改革局招聘公益性岗位工作人员考前自测高频考点模拟试题附答案详解(突破训练)
- 2025辽宁沈阳科技学院诚聘高层次人才模拟试卷附答案详解(完整版)
- 2025广东广州市越秀区华乐街招聘综合行政执法队协管员1人模拟试卷及答案详解一套
- 2025福建厦门市集美区双塔小学产假顶岗教师招聘1人考前自测高频考点模拟试题参考答案详解
- 2025年甘肃兰州事业单位预计4月底5月初发布考前自测高频考点模拟试题附答案详解(完整版)
- 抖音超火看表情符号猜成语课件
- 曾奇峰精神分析初级50讲讲义
- 卡尔曼(Kalman)滤波课件
- 《中国少数民族音乐》教学设计
- 非居民金融账户涉税信息尽职调查管理办法专题培训广州课件
- 科技法庭使用手册汇总
- 生态系统服务功能PPT通用课件
- 红色卡通风小学生大队委竞选PPT模板
- 研究生学籍登记表
- 运动障碍患者的护理
- 泰国英文介绍
评论
0/150
提交评论