您的位置:首页 > 编程语言 > Java开发

Java程序员从笨鸟到菜鸟之(七十四)细谈Spring(六)spring之AOP基本概念和配置详解

2012-06-11 11:11 1036 查看
首先我们来看一下官方文档所给我们的关于AOP的一些概念性词语的解释:
切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。在Spring AOP中,切面可以使用基于模式)或者基于Aspect注解方式来实现。通俗点说就是我们加入的切面类(比如log类),可以这么理解。
连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。在Spring AOP中,一个连接点总是表示一个方法的执行。通俗的说就是加入切点的那个点
通知(Advice):在切面的某个特定的连接点上执行的动作。其中包括了“around”、“before”和“after”等不同类型的通知(通知的类型将在后面部分进行讨论)。许多AOP框架(包括Spring)都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。
切入点(Pointcut):匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例如,当执行某个特定名称的方法时)。切入点表达式如何和连接点匹配是AOP的核心:Spring缺省使用AspectJ切入点语法。
引入(Introduction):用来给一个类型声明额外的方法或属性(也被称为连接类型声明(inter-type declaration))。Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。例如,你可以使用引入来使一个bean实现IsModified接口,以便简化缓存机制。
目标对象(Target Object): 被一个或者多个切面所通知的对象。也被称做被通知(advised)对象。 既然Spring AOP是通过运行时代理实现的,这个对象永远是一个被代理(proxied)对象。
AOP代理(AOP Proxy):AOP框架创建的对象,用来实现切面契约(例如通知方法执行等等)。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。
织入(Weaving):把切面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。

通知类型:
前置通知(Before advice):在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
后置通知(After returning advice):在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
异常通知(After throwing advice):在方法抛出异常退出时执行的通知。
最终通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
环绕通知(Around Advice):包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它自己的返回值或抛出异常来结束执行。
环绕通知是最常用的通知类型。和AspectJ一样,Spring提供所有类型的通知,我们推荐你使用尽可能简单的通知类型来实现需要的功能。例如,如果你只是需要一个方法的返回值来更新缓存,最好使用后置通知而不是环绕通知,尽管环绕通知也能完成同样的事情。用最合适的通知类型可以使得编程模型变得简单,并且能够避免很多潜在的错误。比如,你不需要在JoinPoint上调用用于环绕通知的proceed()方法,就不会有调用的问题。

spring AOP的实现
在spring2.5中,常用的AOP实现方式有两种。第一种是基于xml配置文件方式的实现,第二种是基于注解方式的实现。接下来,以具体的示例来讲解这两种方式的使用。下面我们要用到的实例是一个注册,就有用户名和密码,我们利用AOP来实现在用户注册的时候实现在保存数据之前和之后或者是抛出异常时,在这些情况下都给他加上日志。在这里我们只讲解AOP,所以我只把关键代码贴出来,不相干的就不贴了。

首先我们来看一下业务逻辑service层:

/**  * RegisterService的实现类  * @author 曹胜欢 */ public class RegisterServiceImpl implements RegisterService {     private  RegisterDao registerDao;     public RegisterServiceImpl() {}     /** 带参数的构造方法 */     public RegisterServiceImpl(RegisterDao  registerDao){         this.registerDao =registerDao;     }     public void save(String loginname, String password) {         registerDao.save(loginname, password);         throw new RuntimeException("故意抛出一个异常。。。。");     }       /** set方法 */     public void setRegisterDao(RegisterDao registerDao) {         this.registerDao = registerDao; }}


对于业务系统来说,RegisterServiceImpl类就是目标实现类,它的业务方法,如save()方法的前后或代码会出现异常的地方都是AOP的连接点。

下面是日志服务类的代码:

/**  * 日志切面类  * @author 曹胜欢  */ public class LogAspect {     //任何通知方法都可以将第一个参数定义为 org.aspectj.lang.JoinPoint类型      public void before(JoinPoint call) {         //获取目标对象对应的类名         String className = call.getTarget().getClass().getName();         //获取目标对象上正在执行的方法名         String methodName = call.getSignature().getName();         System.out.println("前置通知:" + className + "类的" + methodName + "方法开始了");     }     public void afterReturn() {         System.out.println("后置通知:方法正常结束了");     }     public void after(){         System.out.println("最终通知:不管方法有没有正常执行完成,一定会返回的");     }     public void afterThrowing() {         System.out.println("异常抛出后通知:方法执行时出异常了");     }     //用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型     public Object doAround(ProceedingJoinPoint call) throws Throwable {         Object result = null;         this.before(call);//相当于前置通知         try {             result = call.proceed();             this.afterReturn(); //相当于后置通知         } catch (Throwable e) {             this.afterThrowing();  //相当于异常抛出后通知             throw e;         }finally{             this.after();  //相当于最终通知         }         return result;     } }


这个类属于业务服务类,如果用AOP的术语来说,它就是一个切面类,它定义了许多通知。Before()、afterReturn()、after()和afterThrowing()这些方法都是通知。

下面我们就来看具体配置,首先来看一下:
<1>.基于xml配置文件的AOP实现:这种方式在实现AOP时,有4个步骤。

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:aop="http://www.springframework.org/schema/aop"         xsi:schemaLocation="             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>     <bean id="registerDaoImpl" class="com.zxf.dao.RegisterDaoImpl"/>     <bean id="registerService" class="com.zxf.service.RegisterServiceImpl">         <property name=" registerDaoImpl " ref=" RegisterDaoImpl "/>     </bean>     <!-- 日志切面类 -->     <bean id="logAspectBean" class="com.zxf.aspect.LogAspect"/>     <!-- 第1步: AOP的配置 -->     <aop:config>         <!-- 第2步:配置一个切面 -->         <aop:aspect id="logAspect" ref="logAspectBean">             <!-- 第3步:定义切入点,指定切入点表达式 -->             <aop:pointcut id="allMethod"                  expression="execution(* com.zxf.service.*.*(..))"/>              <!-- 第4步:应用前置通知 -->             <aop:before method="before" pointcut-ref="allMethod" />             <!-- 第4步:应用后置通知 -->             <aop:after-returning method="afterReturn" pointcut-ref="allMethod"/>             <!-- 第4步:应用最终通知 -->             <aop:after method="after" pointcut-ref="allMethod"/>             <!-- 第4步:应用抛出异常后通知 -->             <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>             <!-- 第4步:应用环绕通知 -->             <!--              <aop:around method="doAround" pointcut-ref="allMethod" />              -->         </aop:aspect>     </aop:config> </beans>


上述配置针对切入点应用了前置、后置、最终,以及抛出异常后通知。这样在测试执行RegisterServiceImpl类的save()方法时,控制台会有如下结果输出:

前置通知:com.zxf.service.RegisterServiceImpl类的save方法开始了。
针对MySQL的RegisterDao实现中的save()方法。
后置通知:方法正常结束了。
最终通知:不管方法有没有正常执行完成,一定会返回的。

下面我们在来看一下第二种配置方式:

<2>基于注解的AOP的实现

首先创建一个用来作为切面的类LogAnnotationAspect,同时把这个类配置在spring的配置文件中。
在spring2.0以后引入了JDK5.0的注解Annotation的支持,提供了对AspectJ基于注解的切面的支持,从而 更进一步地简化AOP的配置。具体的步骤有两步。

Spring的配置文件是如下的配置:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:aop="http://www.springframework.org/schema/aop"         xsi:schemaLocation="             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>     <bean id="registerDao" class="com.zxf.dao.RegisterDaoImpl"/>     <bean id="registerService" class="com.zxf.service.RegisterServiceImpl">         <property name="registerDao" ref="registerDao"/>     </bean>     <!-- 把切面类交由Spring容器来管理 -->     <bean id="logAspectBean" class="com.zxf.aspect.LogAnnotationAspect"/>     <!-- 启用spring对AspectJ注解的支持 -->     <aop:aspectj-autoproxy/> </beans>


这是那个切面的类LogAnnotationAspect

/**  * 日志切面类  */ @Aspect  //定义切面类 public class LogAnnotationAspect {     @SuppressWarnings("unused")     //定义切入点,提供一个方法,这个方法的名字就是改切入点的id     @Pointcut("execution(* com.zxf.service.*.*(..))")     private void allMethod(){}     //针对指定的切入点表达式选择的切入点应用前置通知     @Before("execution(* com. zxf.service.*.*(..))")     public void before(JoinPoint call) {         String className = call.getTarget().getClass().getName();         String methodName = call.getSignature().getName();         System.out.println("【注解-前置通知】:" + className + "类的"                  + methodName + "方法开始了");     }     //访问命名切入点来应用后置通知     @AfterReturning("allMethod()")     public void afterReturn() {         System.out.println("【注解-后置通知】:方法正常结束了");     }     //应用最终通知     @After("allMethod()")     public void after(){         System.out.println("【注解-最终通知】:不管方法有没有正常执行完成,"                  + "一定会返回的");     }     //应用异常抛出后通知     @AfterThrowing("allMethod()")     public void afterThrowing() {         System.out.println("【注解-异常抛出后通知】:方法执行时出异常了");     }     //应用周围通知     //@Around("allMethod()")     public Object doAround(ProceedingJoinPoint call) throws Throwable{         Object result = null;         this.before(call);//相当于前置通知         try {             result = call.proceed();             this.afterReturn(); //相当于后置通知         } catch (Throwable e) {             this.afterThrowing();  //相当于异常抛出后通知             throw e;         }finally{             this.after();  //相当于最终通知         }         return result;     } }

备注:输出结果和前面的一样。

本文出自 “曹胜欢” 博客,请务必保留此出处http://javacsh.blog.51cto.com/3545281/1129144
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐