版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年中专建筑测量模拟试题及答案详解
- 口腔颌面外科模拟试题及答案详解
- 2026年信息科技大赛模拟试题及答案详解
- 河南省信阳市名校2027届数学七年级第一学期期末经典模拟试题含解析
- 2026年江西省国有档案馆招聘考试练习试卷(含答案)
- 2026年四川国有档案馆预算核算招聘考试练习试卷(含答案)
- 2026年山西孝义银行招聘考试练习试卷(含答案)
- 2026年四川雅安文物保护中心招聘考试练习试卷(含答案)
- 河北省唐山市龙华中学2027届数学八年级第一学期期末经典模拟试题含解析
- 2026年国有金融企业中层管理竞聘考试题库
- 2026小学苏教版五年级科学上册全册教案
- 顶板管理专项安全技术措施培训课件
- 畜牧养殖场智能化生产系统方案
- 中大考博骨科试题及答案
- 山东国惠(香港)控股有限公司招聘笔试题库2026
- 2026年社会保障卡知识竞赛题库(附答案)
- 正畸科普流程
- 中石油承包商培训课件
- 教育教学评价
- 19 11 坐井观天-坐井观天(第2课时)-课件2
- 220kV输电线路事故应急预案
评论
0/150
提交评论