付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Spring
第二课大纲AOP入门Spring2.0的AOP支持AOP入门
在一个服务的流程中插入与业务逻辑无关的系统服务逻辑(例如Logging、Security),这样的逻辑称为Cross-cuttingconcerns,将Cross-cuttingconcerns独立出来设计为一个对象,这样的特殊对象称之为Aspect,Aspect-orientedprogramming着重在Aspect的设计上以及与应用程序的织入(Weave)。
AOP跟OOP(Object-orientedprogramming)并不相互抵触,它们是可以相辅相成的两个设计模型,SpringAOP是实现AOP的一种技术,而SpringAOP也是Spring中一些子框架或子功能所依赖的核心。AOP入门 AOP-Aspect-orientedprogramming
从代理机制初探AOP
如想在执行HelloSpeaker的hello()方法时留下日志消息,可以在hello()方法内部写log,或者说将执行log的程序横切入(Cross-cutting)到HelloSpeaker,但这样就使得HelloSpeaker关注了不属于它自己的业务逻辑。如是可以采取代理的机制。
代理(Proxy)有两种: 静态代理-Staticproxy
动态代理-DynamicproxyAOP入门1.静态代理 为实现静态代理需要为HelloSpeaker写一个HelloProxy类,同样实现IHello接口,并在hello方法执行log,并执行HelloSpeaker的hello()方法。
publicvoidhello(Stringname){ log(“hellomethodstart”); helloObject.hello(name); log(“hellomethodend….”); }其中helloObject为需要代理的对象,在其它地方如下来使用代理机制:IHelloproxy=newHelloProxy(newHelloSpeaker());proxy.hello(“test”);AOP入门代理机制动作流程AOP入门2.动态代理importjava.util.logging.*;importjava.lang.reflect.*;
publicclassLogHandlerimplementsInvocationHandler{privateLoggerlogger=Logger.getLogger(this.getClass().getName());
privateObjectdelegate;publicObjectbind(Objectdelegate){this.delegate=delegate;returnProxy.newProxyInstance(delegate.getClass().getClassLoader(),delegate.getClass().getInterfaces(),this);}AOP入门publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)throwsThrowable{Objectresult=null;try{log("methodstarts..."+method);
result=method.invoke(delegate,args);
logger.log(Level.INFO,"methodends..."+method);}catch(Exceptione){log(e.toString());}returnresult;}
privatevoidlog(Stringmessage){logger.log(Level.INFO,message);}}AOP入门测试程序如下:publicstaticvoidmain(String[]args){LogHandlerlogHandler=newLogHandler();
IHellohelloProxy=(IHello)logHandler.bind(newHelloSpeaker());helloProxy.hello("Justin");}AOP入门
通过代理机制,HelloSpeaker不用关注与其本身业务逻辑无关的事情。用AOP的术语来说,日志的程序横切(Cross-cutting)入HelloSpeaker的程序执行流程中,日志这样的动作在AOP中称之为横切关注点(Cross-cuttingconcern)
使用代理对象将日志等与业务逻辑无关的动作或任务提取出来,设计成一个服务对象,像之前范例中示范的HelloProxy或是LogHandler,这样的对象称之为切面(Aspect)。
AOP中的Aspect所指的像日志等这类的动作或服务,将这些动作(Cross-cuttingconcerns)设计为通用、不介入特定业务对象的一个职责清楚的Aspect对象,这就是所谓的Aspect-orientedprogramming,即AOP。AOP入门 AOP-面向切面/方面的编程
AOP:是在一个在切面上编程,弥补OOP不足的一种编程思想,可以在顺序执行的程序中,插入某些特殊的逻辑来实现一些特殊的功能; 总体来说:OOP提高了代码的重用,设计模式解决了模块之间的耦合,AOP解决了某个模块内部的变化问题。
AOP把软件系统分为两部分:
1、核心关注点(业务处理为主)2、横切关注点(与业务无关如日志等)
核心思想:将商业逻辑同对其提供支持的通用服务进行分离;AOP入门AOP基本概念*切面(Aspect)
将散落于各个业务逻辑之中的Cross-cuttingconcerns收集起来,设计成各个独立可重用的对象,这样的对象称之为Aspect。*通知(Advice) Aspect当中对Cross-cuttingconcerns的具体实现称之为Advice。Advice中包括了Cross-cuttingconcerns的行为或所要提供的服务。*连接点(Joinpoint) Advice在应用程序执行时加入业务流程的点或时机。*切入点(Pointcut)
定义了感兴趣的Jointpoint,当调用的方法符合Pointcut表达式时,将Advice织入至应用程序上提供服务。AOP入门*目标对象(TargetObject)
一个Advice被应用的对象或目标对象,如前面的HelloSpeaker就是LogHandler中Advice的Target*引入(Introduction)
在不修改原程序代码的情况下,为现存的类引入新的方法或行为*AOP代理(AOPProxy)
是由AOP框架创建的对象,用来实现切面契约(包括通知方法执行等功能)*织入(Weave) Advice被应用到对象之上的过程称之为织入(Weave)AOP入门AspectSecurityHandlerLogerHandlerPointcut________Cross-cutting被辨识为
JointpointJointpointJointpointconcernsWeave根据具体设计为AdvicesSpring2.0的AOP支持在Spring2.0之后,对AOP功能的实现与设置新增了两种方式:一种是基于XMLSchema的设置,一种是基于Annotation的支持,两种方式对于AOP在使用上的简化都有极大的帮助。Spring2.0的AOP支持BeforeAdvice:基本XMLSchemapublicclassLogBeforeAdvice{privateLoggerlogger=Logger.getLogger(this.getClass().getName());
publicvoidbefore(JoinPointjointPoint){logger.log(Level.INFO,"methodstarts..."+jointPoint.getSignature().getDeclaringTypeName()+"."+jointPoint.getSignature().getName());}}Spring2.0的AOP支持
在程序代码中,before()是任意取的名称,它可以接受JointPoint实例,也可以省略。可以使用JointPoint的getTarget()方法取得目标对象,使用getArgs()取和调用的方法参数,使用getSignature()方法取得Pointcut签名等信息。在Spring2.0中,任何一个Advice的方法实现都可以自行决定是否接受JointPoint实例。Spring2.0的AOP支持Spring2.0的xml名称空间声明:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns=""xmlns:xsi=""xmlns:aop=""xsi:schemaLocation="/spring-beans-2.0.xsd
/spring-aop-2.0.xsd">Spring2.0的AOP支持AOP标签申明
<aop:config> <aop:aspectid="logging"ref="logBeforeAdvice"> <aop:beforepointcut="execution(*net.socloud.IHello.*(..))" method="before"/> </aop:aspect></aop:config>如果需要重用Pointcut定义,可以使用<aop:pointcut>标签<aop:pointcutid=“logHello”expression=“execution(*net.socloud.IHello.*(..))"/>引用使用:<aop:beforepointcut-ref=“logHello”method=“before”/>Spring2.0的AOP支持BeforeAdvice:基本Annotation@AspectpublicclassLogBeforeAdvice{privateLoggerlogger=Logger.getLogger(this.getClass().getName());
@Before(“execution(*net.socloud.IHello.*(..))”) publicvoidbefore(JoinPointjointPoint){ }} Xml配置文件中使用<aop:aspectj-autoproxy/>标签来启用@Aspect的Annotation支持Spring2.0的AOP支持Spring2.0的Pointcut定义execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
它们分别表示存取修饰匹配,传回值类型匹配,类类型匹配,方法名称匹配,异常类型匹配,有问号的部分,表示可以省略不声明。Spring2.0的AOP支持大部分情况下,传回值类型匹配会使用“*”,表示所有传回值类型都符合,也可以设置完整类型名称。方法名称匹配也可以使用“*”。参数类型匹配,用()表示没有参数,(type)表示带有一个类型为type的参数;(*)表示带有一个参数,而该参数可是任意类型;也可以设置如(*,String),表示有两个参数,第一个参数可以是任意类型,而第二个参数是String;(..)表示零个或多个参数。execution(public**(..))//所有public方法,不要使用这个execution(*hello*(..))execution(*net.socloud.IHello.*(..))execution(*net.socloud.*.*(..))execution(*net.socloud..*.*(..))//包括子包在内任何方法Spring2.0的AOP支持结合@Pointcut的Annotation定义方式,可以让Pointcut在定义上更加容易,定义包括两个部分:Pointcut表示式与Pointcut签名(signature)。例如以下定义一个符合hello开头方法的Pointcut@Pointcut(“execution(*hello*(..))”)privatevoidanyHello();@Before(“anyHello()”)privatevoide();Pointcut定义时,还可以使用&&,||,!运算。Spring2.0的AOP支持AfterReturningAdvice:基于XMLSchema<aop:aspectid="logging"ref="logAspect"><aop:pointcutid="logHello"expression="execution(*net.socloud.IHello.*(..))"/> <aop:beforepointcut-ref="logHello"method="before"/>
<aop:after-returning pointcut-ref="logHello" method="afterReturning"/></aop:aspect>Spring2.0的AOP支持AfterReturningAdvice:基于Annotation@Pointcut("execution(*net.socloud.IHello.*(..))")privatevoidlogging(){}
@Before("logging()")publicvoidbefore(JoinPointjointPoint){
}@AfterReturning(pointcut="logging()",returning="retVal")publicvoidafterReturning(JoinPointjointPoint,ObjectretVal){logger.log(Level.INFO,"methodends..."+jointPoint.getSignature().getDeclaringTypeName()+"."+jointPoint.getSignature().getName());}Spring2.0的AOP支持AfterThrowingAdvice:基于XMLSchema<aop:config> <aop:aspectid="logging"ref="logAspect"> <aop:pointcutid="logHello"expression="execution(*net.socloud.IHello.*(..))"/> <aop:after-throwing pointcut-ref="logHello" throwing="throwable" method="afterThrowing"/>
</aop:aspect></aop:config>Spring2.0的AOP支持AfterThrowingAdvice:基于Annotation@Pointcut("execution(*net.socloud.IHello.*(..))")privatevoidlogging(){}
@AfterThrowing(pointcut="logging()",throwing="throwable")publicvoidafterThrowing(JoinPointjointPoint,Throwablethrowable){logger.log(Level.INFO,"Loggingthata"+throwable+"\nExceptionwasthrownin..."+jointPoint.getSignature().getDeclaringTypeName()+"."+jointPoint.getSignature().getName());}Sp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年湖南单招汽车维修专业中职生技能考核题库含故障诊断
- 2026年浙江省事业单位考试综合应用能力模拟题含答案
- 2026年机关干部政务移动应用程序管理新规测试
- 2026年企业财务管理及财务分析试题
- 2026年青少年节约粮食行为养成题库
- 劳动防护用品佩戴使用检查规范
- 2026年检验技师入职操作规范自测题
- 产品市场分析与消费者行为研究
- 2026年技术革新与科技创新试题库
- 2026年中国电信网络安全公司招聘考试试题
- 《分析人类活动对生态环境的影响》生物教学课件
- 2026中国背景音乐系统行业应用态势与盈利前景预测报告
- 2026年体育教师招聘考试真题及答案
- 义务教育均衡发展质量监测八年级综合试卷(附答案)
- 2025年江西移动第四季度社会招聘笔试历年典型考点题库附带答案详解
- AQ 2084-2025 陆上石油天然气井下作业安全规范
- 宠物美容师就业合同协议(2025年工作规范)
- 珍珠的漂白处理 2
- 某工程甘肃段地质灾害危险性评估报告
- 节后复工复产安全隐患排查表
- GB/T 2828.10-2010计数抽样检验程序第10部分:GB/T 2828计数抽样检验系列标准导则
评论
0/150
提交评论