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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

Spring Aop入门.docx

1、Spring Aop入门Spring-Aop入门AOP全名Aspect-oriented programming。Springframework是很有前途的AOP技术。作为一种非侵略性的,轻型的AOP framework,你无需使用预编译器或其他的元标签,便可以在Java程序中使用它。这意味着开发团队里只需一人要对付AOP framework,其他人还是像往常一样编程。关键性概念:1)Advice是代码的具体实现,例如一个实现日志记录的代码。2)Pointcut是在将Advice插入到程序的条件。3)advisor是把pointcut和advice的组合在一起装配器。图例:你的程序可能如上,现

2、在要在三个流程上同时加入日志控制和权限控制,如下:你的程序可能如上,现在要在三个流程上同时加入日志控制和权限控制,如下:其中拿日志为例,日志控制和流程之间的穿插点处叫做连接点(Joinpoint),而Advice就是我们日志处理的具体代码,Pointcut就是定义一个规则,对三个和业务有关的连接点进行过滤和匹配(例如我们对于业务1不做日志处理)。Advisor就是将符合的规则的剩下的两个连接点和具体的日志记录代码组合在一起。三.Spring-Aop前置通知、后置通知、环绕通知、异常通知实现我以SpringIn Action提供的例子进行二次改造,完成我们自己的流程。业务流程很简单,顾客到商店买

3、东西,店员根据顾客的需要给于顾客提供服务。实现方法前插入,方法后插入,环绕,异常四种。代码: 建立一个用户类;public class Customer private String name = 悠游!; public String getName() return name; 三个产品public class Cheese public String toString() return 奶酪!; public class Pepper public String toString() return 胡椒粉!; public class Squish public String toStri

4、ng() return 果酱!; 建立一个接口;public interface KwikEMart Squish buySquish(Customer customer) throws KwikEMartException; Pepper buyPepper(Customer customer) throws KwikEMartException; Cheese buyCheese(Customer customer) throws KwikEMartException;实现这个接口,我们实现三个方法,买奶酪,买胡椒粉,买果酱;public class ApuKwikEMart implem

5、ents KwikEMart private boolean cheeseIsEmpty = false; private boolean pepperIsEmpty = false; private boolean squishIsEmpty = false; public Cheese buyCheese(Customer customer) throws NoMoreCheeseException if (cheeseIsEmpty) throw new NoMoreCheeseException(); Cheese s = new Cheese(); System.out.printl

6、n(-我想买: + s); return s; public Pepper buyPepper(Customer customer) throws NoMorePepperException if (pepperIsEmpty) throw new NoMorePepperException(); Pepper s = new Pepper(); System.out.println(-我想买: + s); return s; public Squish buySquish(Customer customer) throws NoMoreSquishException if (squishIs

7、Empty) throw new NoMoreSquishException(); Squish s = new Squish(); System.out.println(-我想买: + s); return s; public void setCheeseIsEmpty(boolean cheeseIsEmpty) this.cheeseIsEmpty = cheeseIsEmpty; public void setPepperIsEmpty(boolean pepperIsEmpty) this.pepperIsEmpty = pepperIsEmpty; public void setS

8、quishIsEmpty(boolean squishIsEmpty) this.squishIsEmpty = squishIsEmpty; 环绕通知的实现,必须实现invoke方法,通过调用invoke.proceed()手工调用对象方法:public class OnePerCustomerInterceptor implements MethodInterceptor private Set customers=new HashSet(); public Object invoke(MethodInvocation invoke) throws Throwable Customer c

9、ustomer=(Customer)invoke.getArguments()0; if(customers.contains(customer) throw new KwikEMartException(One per customer.); System.out.println(店员:+customer.getName() + ,Can I help you ?); Object squishee=invoke.proceed(); /手工调用对象方法; System.out.println(店员:OK! + customer.getName() + .give you! ); custo

10、mers.add(squishee); return squishee; 前置通知的实现;public class WelcomeAdvice implements MethodBeforeAdvice public void before(Method method, Object args, Object target) throws Throwable Customer customer = (Customer) args0; System.out.println(店员:Hello + customer.getName() + . How are you doing?); public

11、class Customer private String name = 悠游!; public String getName() return name; 后置通知实现;public class ThankYouAdvice implements AfterReturningAdvice public void afterReturning(Object returnValue, Method method, Object args, Object target) throws Throwable Customer customer = (Customer) args0; System.ou

12、t.println(店员:Thank you + customer.getName() + . Come again! ); 系统异常处理通知实现;public class KwikEmartExceptionAdvice implements ThrowsAdvice public void afterThrowing(NoMoreSquishException e) System.out.println(系统:NoMoreSquisheesException异常截获了: + e.getMessage(); public void afterThrowing(NoMoreCheeseExce

13、ption e) System.out.println(系统:NoMoreCheeseException异常截获了: + e.getMessage(); public void afterThrowing(NoMorePepperException e) System.out.println(系统:NoMorePepperException异常截获了: + e.getMessage(); 自定义的异常接口;public class KwikEMartException extends Exception private static final long serialVersionUID =

14、-3962577696326432053L; String retValue = KwikEMartException异常!; public KwikEMartException(String name) retValue = name; public KwikEMartException() public String getMessage() return retValue; 没有更多的奶酪异常;public class NoMoreCheeseException extends KwikEMartException private static final long serialVers

15、ionUID = -3961123496322432053L; String retValue = NoMoreCheeseException异常!; public NoMoreCheeseException() super(); public NoMoreCheeseException(String name) super(name); public String getMessage() return retValue; 没有更多的胡椒粉异常;public class NoMorePepperException extends KwikEMartException private stat

16、ic final long serialVersionUID = -3961234696322432053L; String retValue = NoMorePepperException异常!; public NoMorePepperException() super(); public NoMorePepperException(String name) super(name); public String getMessage() return retValue; 没有更多的果酱异常;public class NoMoreSquishException extends KwikEMar

17、tException private static final long serialVersionUID = -3121234696322432053L; String retValue = NoMoreSquishException异常!; public NoMoreSquishException() super(); public NoMoreSquishException(String name) super(name); public String getMessage() return retValue; 运行实例类;public class RunDemo public stat

18、ic void kwikEMart() ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(demo/kwikemart.xml); /如果你想通过类来引用这个的话,就要用到CGLIB.jar了,同时在代理工厂里面设置: / KwikEMart akem = (KwikEMart) context.getBean(kwikEMart); try akem.buySquish(new Customer(); akem.buyPepper(new Customer(); akem.buyCheese

19、(new Customer(); catch (KwikEMartException e) /异常已经被截获了,不信你看控制台!; public static void main(String args) kwikEMart(); Xml文件配置: !- 把这里注释去掉的话,程序调用的时候测试异常通知; true true true - welcomeAdvice thankYouAdvice onePerCustomerInterceptor kwikEmartExceptionAdvice 这个例子东西很多,不过每个类的代码都不大。如果你对org.springframework.aop.f

20、ramework.ProxyFactoryBean不是很了解的话可以看我下篇尾处的介绍。读清楚之后,我们运行RunDemo 类,查看控制台结果,如下:店员:Hello 悠游! . How are you doing?店员:悠游! ,Can I help you ?-我想买:果酱!店员:OK! 悠游!.give you!店员:Thank you 悠游! . Come again!店员:Hello 悠游! . How are you doing?店员:悠游! ,Can I help you ?-我想买:胡椒粉!店员:OK! 悠游!.give you!店员:Thank you 悠游! . Come again!店员:Hello 悠游! . How are you doing?店员:悠游! ,Can I help you ?-我想买:奶酪!店员:OK! 悠游!.give you!店员:Thank you 悠游! . Come again!我们将kwikEMartTarget里面的

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1