ImageVerifierCode 换一换
格式:DOC , 页数:11 ,大小:204KB ,
资源ID:3520629      下载积分:20 文钱
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,省得不是一点点
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.wenke99.com/d-3520629.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Spring-AOP.doc)为本站会员(sk****8)主动上传,文客久久仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知文客久久(发送邮件至hr@wenke99.com或直接QQ联系客服),我们立即给予删除!

Spring-AOP.doc

1、Spring AOPMade by Michael.S全文以超市售货这个例子来演示 SpringAOP 的实现。其中包括:POJO(Customer, Squishee) ,接口 WallsMart(其中有方法 Squishee buySquishee(Customer customer),接口实现WallsMart_Xian,Advice(包括 Before, afterReturning)AOP-API 实现Advice 介绍Before Advice需要实现MethodBeforeAdvice接口public interface MethodBeforeAdvice void befor

2、e(Method method, Object args, Object target)throws ThrowableMethod 是目标方法,args 是参数,target 是方法调用的目标对象其中,args 和 target 对象是不能够改变的。实现:public class WelcomeAdvice implements MethodBeforeAdviceOverridepublic void before(Method method, Object args, Object target)Customer customer = (Customer)args0;System.out

3、.println(“Hello, “ + customer.getName() + “! Welcome .“);在 xml 中配置com.primeton.spl.WallsMartadvice(1).配置被代理对象 bean: wallsMartTarget(2).配置 Advise bean: welcomeAdvice(3).配置代理类 bean: wallsMart其中:属性 proxyInterfaces 是指代理的接口属性 interceptorNames 是指要拦截的方法名字(在类 Advice 中可以包括before,afterReturning 等方法,这样都会进行拦截)属

4、性 target 是指被代理对象wallsMart 这个 bean 的功能即:(1).实现 wallsMart 这个接口(2).应用 welcomeAdvice 这个对象到所有的调用中(3).将 WallsMart_Xian 设定为目标对象Comment Mike1: 需要引入aopalliance.jarAfterAdvice需要实现AfterReturningAdvice接口public interface AfterReturningAdvice void afterReturning(Object returnValue, Method method,Object args, Obje

5、ct target) throws ThrowableOverridepublic void afterReturning(Object arg0, Method arg1, Object arg2,Object arg3) throws ThrowableSystem.out.println(“This is After advice . “);其中参数 returnValue是指目标方法的返回值AroundAdvice需要实现MethodInterceptor接口public interface MethodInterceptor extends Interceptor Object in

6、voke(MethodInvocation invocation) throws Throwable;注意:(1). MethodInterceptor控制着目标方法是否被调用(通过调用MethodInvocation.proceed());(2). MethodInterceptor给你设定返回值的权力。你可以返回你想要的返回值(类型必须相同),通过调用 proceed()方法。class AroundAdvice implements MethodInterceptorOverridepublic Object invoke(MethodInvocation arg0) throws Th

7、rowableSystem.out.println(“Before“);Object obj = arg0.proceed();System.out.println(“After“);return obj;ThrowAdvice这是一个Marker 接口,没有任何方法需要被实现, Advice类实现该接口只需要至少实现下列两个方法之一即可:void afterThrowing(Throwable throwable)void afterThrowing(Method method, Object args, Object target,Throwable throwable)参数throwab

8、le就是指所抛出异常。你也可以在一个Advice类中含有多个afterThrowing方法用来区分对待不同的异常信息。class ExceptionAdvice implements ThrowsAdvicepublic void afterThrowing(NullPointerException eNull)System.out.println(“Null Pointer .“);public void afterThrowing(RuntimeException eRT)System.out.println(“Run Time .“);注意:上述两个方法不能够捕获和处理异常,异常信息会继

9、续传播下去。PointCutPointcut根据方法和类判断哪里需要织入。public interface Pointcut ClassFilter getClassFilter();MethodMatcher getMethodMatcher();其中:public interface ClassFilter boolean matches(Class clazz);public interface MethodMatcher boolean matches(Method m, Class targetClass);public boolean isRuntime();public bool

10、ean matches(Method m, Class target, Object args);推荐使用静态 PointcutAdvisor是pointcut 和 advice的结合。public interface PointcutAdvisor Pointcut getPointcut();Advice getAdvice();(1). NameMatchMethodPointcutAdvisor类中有两个方法:public void setMappedName(String)public void setMappedNames(String)根据方法名字进行拦截Beans.xml 文件

11、配置:.配置 advice 的 bean,包含 before,afterReturning 等 advice 方法:.配置 advisororder*可以使用通配符*来进行匹配.配置 ProxyFactoryBeancom.primeton.spl.cleaning.MaidServicefrequentCustomerPointcutAdvisor与上文中提到的 Before 不同的地方在于:Before:Advisor:这样会降低耦合,而且会更加灵活。(2). RegexpMethodPointutAdvisor正则表达式实现方法Xml 文件配置只需要将上述 advisor 改变即可:.*

12、get.*AOP-Annotation 实现1. 启动 aop 支持(1).普通 xml 格式(2).DTD 格式2. 声明 AspectAspectpublic class FrequentCustomerAdvice在 Advice 类上面声明该类为 aspect 类3. 声明 pointcut,设置 before,after 等方法Before(“execution( * com.primeton.spl.cleaning.MaidService.*(.)“)public void before()System.out.println(“Before Advice . “);注意:Aro

13、und 方法 需要有一个参数来使得方法运行Around(“execution( * com.primeton.spl.cleaning.MaidService.*(.)“)public void around(ProceedingJoinPoint pjp) throws ThrowableSystem.out.println(“Before method“);pjp.proceed();System.out.println(“End method“);AOP-XML 实现1. 配置 Aspect2. 配置 pointcut3. 配置 before,after注意:这时的 before 可以不用像 API 实现方法那样继承那些接口;如果 before等方法有参数,则需要在 execution 表达式中进行设置。public void before()System.out.println(“Before Advice . “);全局 pointcut

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。