Spring的AOP模块.doc

  1. 1、本文档共17页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Spring的AOP模块.doc

第3章 Spring的AOP模块 本章学习目的和要求 本章重点和难点 AOP是Spring框架的另一个重要特征。AOP(Aspect Oriented Programming,面向切面编程)把一个业务流程分成几部分,例如权限检查、业务处理、日志记录,每个部分单独处理,然后把它们组装成完整的业务流程。每个部分被称为切面(Aspect)或者关注点。 3.1 实例:使用拦截器拦截方法(在struts-2.3.8中复制commons-logging-1.1.1、aopalliance-1.0.jar) AOP有一些重要的概念,包括切面(Aspect)、连接点(Joinpoint)、通知(Advice)、切入点(Pointcut)、引用(Introduction)等。这些概念并不是Spring定义的。对于刚刚接触AOP的开发者来说,这些概念非常抽象,往往很难理解。 抛开这些抽象的概念,先来看一个AOP例子。前面的章节中曾介绍了一个AOP的例子。下面看一下稍微复杂的例子。本例中,AOP的规则要稍微复杂一些。 3.1.1 Service接口 Spring推荐使用接口编程。该接口定义了两个方法。一会儿将使用拦截器拦截其中的withAop()方法,而另一个方法withoutAop()将不会被拦截。 IAopService.java package com.helloweenvsfei.spring.aop; public interface IAopService { //Service接口,定义两个方法 public void withAop() throws Exception; //将会被拦截 public void withoutAop() throws Exception; //不会被拦截 } 3.1.2 Service实现代码 IAopService实现类中定义了一个name属性,以及对应的getter、setter方法。实现类代码为: AopServiceImpl.java package com.helloweenvsfei.spring.aop; import javax.security.auth.login.AccountException; public class AopServiceImpl implements IAopService { //Service实现 private String name; //name属性 public void withAop() throws Exception { //withAop方法实现 System.out.println(有AOP的函数运行。name: +name); if (name.trim().length()==0) // 如果name为空 throw new AccountException(name属性不能为空); // 则抛出异常 } public void withoutAop() throws Exception { // withoutAop方法实现 System.out.println(没有AOP的函数运行。); } public String getName() { //getter方法 return name; } public void setName(String name) { //setter方法 =name; } } 3.1.3 方法前拦截器检查name是否为null 下面是方法前拦截器,实现自MethodBeforeAdvice接口。方法前拦截器在执行指定方法前被调用,参数分别为被调用的方法、执行时被传入的参数、被拦截的Bean。代码为: MethodBeforeInterceptor.java package com.helloweenvsfei.spring.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class MethodBeforeInterceptor implements MethodBeforeAdvice { //方法前拦截器 // 调用对象的方法前将执行该方法。参数分别为被调用的方法、被调用调用方法的参数、对象 public void before(Method method, Object[] args, Object instance) throws Throwable { System.out.println(即将要执行方法: + method.getName(

文档评论(0)

2232文档 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档