




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、本文主要简单的介绍了如何基于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
2、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
3、.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 beforeSayHe
4、llo(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); Ar
5、ound(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
6、(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.H
7、elloWord.sayHello(.)",returning="reval") public void afterReturningSayHello(String reval) System.out.println("After Returning : "+reval); 4.HelloWord.xml<beans xmlns="/schema/beans" xmlns:xsi="/2001/XMLSchema-ins
8、tance" xmlns:aop="/schema/aop" xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.0.xsd /schema/context /schema/context/spr
9、ing-context-3.0.xsd /schema/util /schema/util/spring-util-3.0.xsd /schema/aop /schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <b
10、ean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> <bean id="helloWordAspect" class="com.SpringAOP.HelloWord.HelloWordAspect"></bean> <bean id="helloWord" class="com.SpringAOP.HelloWord.Hello
11、Word"></bean></beans>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() Applic
12、ationContext 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 poin
13、tswithin 指定连接点所在的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 runti
14、me 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表达式可以使用 && 、| 、! 组合poi
15、ntcut表达式。execution格式execution(可见性(可选) 使用public、protected、private指定可见性。也可以为*,表示任意可见性返回值类型(必须) 指定返回值类型声明类型(可选) java包方法名(参数模式)(必须) 方法名称,和方法接收的参数 异常模式(可选) 指定方法签名中是否存在异常类型)在pointcut表达式中可以使用 * 、. 、 +等通配符。* :表示若干字符(排除 . 在外). :表示若干字符(包括 . 在内)+ :表示子类 ,比如Info+ 表示Info类及其子类2.定义Adviceadvice在关联的pointcu
16、t表达式的方法运行前(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匹配的方法抛出异常后,执行
17、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()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年咸阳市税务系统遴选面试真题附详细解析含答案
- 煤矿安全生产目标管理制度
- 领导干部和公务员法律知识考试题库含参考答案
- 2025年内蒙古呼伦贝尔家庭教育协会招聘考试笔试试题(含答案)
- 2025年湖南邵阳北塔区区外选调教师考试笔试试题(含答案)
- 老年护理绪论课件
- 老师课件自我介绍
- 老师安全培训课件
- 老子思想课件
- 材料化学建模合同
- 清水混凝土涂料施工工艺简介
- 安保工作考核表
- 2024年新人教版七年级道德与法治上册全册教学课件
- 电子政务概论-形考任务5(在线测试权重20%)-国开-参考资料
- 古代小说戏曲专题-形考任务2-国开-参考资料
- GB/T 16886.10-2024医疗器械生物学评价第10部分:皮肤致敏试验
- 2023-2024学年曲靖市七年级语文下学期期末考试卷(附答案解析)
- 2024-2030年中国低温超导材料行业市场深度调研及发展前景与投资战略研究报告
- 医院感染管理制度制度汇编
- HG∕T 3642-2016 水处理剂 丙烯酸-2-甲基-2-丙烯酰胺基丙磺酸类共聚物
- 水泵采购投标方案(技术方案)
评论
0/150
提交评论