版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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届数学高一下期末考试试题含解析
- 2025年山东泰安卫生事业编考试及答案
- 2025年山东邹城市事业单位考试及答案
- 2025年海大生物笔试题目及答案
- 2025年服务窗口笔试题及答案
- 2025年设施操作员笔试题目及答案
- 2025年卫生保健考试笔试及答案
- 2025年每一年的事业编考试及答案
- 2025年青海柴达木职业技术学院单招职业适应性考试题库附答案解析
- 2024年焦作师范高等专科学校马克思主义基本原理概论期末考试题附答案解析(必刷)
- 2026年广东省事业单位集中公开招聘高校毕业生11066名笔试模拟试题及答案解析
- 2025年淮北职业技术学院单招职业适应性测试题库带答案解析
- 安全生产九个一制度
- 司法鉴定资料专属保密协议
- (更新)成人留置导尿护理与并发症处理指南课件
- 丝路基金招聘笔试题库2026
- 巨量引擎《2026巨量引擎营销IP通案》
- 2026届高考化学冲刺复习化学综合实验热点题型
- 电缆接驳施工方案(3篇)
- 唐代皇太子教育制度与储君培养
- 中职生理学考试真题及解析
评论
0/150
提交评论