`
iorit2003
  • 浏览: 140900 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

Spring 2.0中的AOP实现

阅读更多
Spring 2.0中,除了传统的通过实现AOP AIP的方式来实现Advice之外,还提供了两种更加简便的方式来实现Advice1)基于XML Schema的设置;2)基于Annotation的支持,采用这两种方式,Advice将不用实现特定的接口。现在让我们来看看如何使用这两种方式来分别实现Before AdviceAfter AdviceAround AdviceThrowing Advice
    一、Before Advice:基于XML Schema
    当基于XML Schema实现Before Advice时,你的Advice类不用实现org.springframework.aop.MethodBeforeAdvice接口,例如:
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4.   
  5. public class LogBeforeAdvice {  
  6.      public void before(JoinPoint joinPoint) {  
  7.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
  8.      }  
  9. }    

        before
方法是在目标对象上的方法被执行前要执行的方法,before方法中的JoinPoint参数是可选项,你可以根据需要决定是否需要JoinPoint参数,通过JoinPoint对象,你可以获得目标对象(getTarget())、目标方法上的参数(getArgs())等信息。
    然后在XML中为目标对象指定LogBeforeAdvice代理:
xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.     http://www.springframework.org/schema/aop  
  8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  10.       
  11.     <bean id="logBeforeAdvice" class="com.savage.aop.LogBeforeAdvice"></bean>  
  12.       
  13.     <aop:config>  
  14.         <aop:aspect id="logBefore" ref="logBeforeAdvice">  
  15.             <aop:before pointcut="execution(* com.savage.aop.MessageSender.*(..))" method="before"/>  
  16.         </aop:aspect>  
  17.     </aop:config>  
  18. </beans>  

    如上所示,在Spring 2.0中要使用基于XML Sechma声明AOP的方式,需要在XML中加入aop的名称空间。当基于XML Sechma实现AOP时,所有的AOP都是在<aop:config></aop:config>标签中声明的,<aop:aspect></aop:aspect>用于定义Advice实例。<aop:before></aop:before>表示当前实例用于实现Before Advicepointcut属性用于指定pointcut表示式,上面的例子表示此Advice将应用于com.savage.aop.MessageSender接口中的任何方法;method属性表示Advice上要调用的方法。
    现在调用任何MessageSender接口上的方法之前都会执行LogBeforeAdvicebefore方法,例如:
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class AdviceDemo {  
  7.     public static void main(String[] args) {  
  8.         ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");  
  9.         MessageSender sender = (MessageSender)context.getBean("messageSender");  
  10.         sender.sendMessage("message");  
  11.     }  
  12. }  

    二、Before Advice:基于Annotation
    使用Annotation来实现Advice,在XML文件上的定义要比基于XML Sechema的方法要简便的多,但在实现Before Advice类时,则需要使用到@Aspect@Before标识,并需要引入org.aspectj.lang.annotation包中的类。还以LogBeforeAdvice为例,LogBeforeAdvice类需要改为:
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.annotation.Aspect;  
  5. import org.aspectj.lang.annotation.Before;  
  6.   
  7. @Aspect  
  8. public class LogBeforeAdvice {  
  9.     @Before("execution(* com.savage.aop.MessageSender.*(..))")  
  10.     public void before(JoinPoint joinPoint) {  
  11.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
  12.     }  
  13. }  

    如上所示,通过@Aspect将一个类声明为Aspect类,通过@Before将方法声明Before Advice,方法中的JoinPoint同样是可选的。然后在XML文件中做如下定义:
xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.     http://www.springframework.org/schema/aop  
  8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  10.       
  11.     <bean id="logBeforeAdvice" class="com.savage.aop.LogBeforeAdvice"></bean>  
  12.       
  13.     <aop:aspectj-autoproxy/>  
  14. </beans>  

    所有基于Annotation实现的Advice,在XML文件中都只要使用<aop:aspectj-autoproxy></aop:aspectj-autoproxy>进行设置就可以了,非常简单。

    三、After Advice:基于XML Sechma
    和Before Advice一样,基于XML Sechma实现After Returning Advice时,不再需要org.springframework.aop.AfterReturningAdvice接口:
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4.   
  5. public class LogAfterReturningAdvice {  
  6.     public void afterReturning(JoinPoint joinPoint) {  
  7.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
  8.     }  
  9. }  

    然后在XML中做如下设置:
xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.     http://www.springframework.org/schema/aop  
  8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  10.       
  11.     <bean id="logAfterReturningAdvice" class="com.savage.aop.LogAfterReturningAdvice"></bean>  
  12.       
  13.     <aop:config>  
  14.         <aop:aspect id="logAfterReturning" ref="logAfterReturningAdvice">  
  15.             <aop:after-returning   
  16.                 pointcut="execution(* com.savage.aop.MessageSender.*(..))"   
  17.                 method="logAfterReturning"/>  
  18.         </aop:aspect>  
  19.     </aop:config>  
  20. </beans>  

    四、After Advice:基于Annotation
    和Before Advice相似,使用@AfterReturning来表示After Returning Advice
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.annotation.Aspect;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6.   
  7. @Aspect  
  8. public class AfterReturningAdvice {  
  9.     @AfterReturning(pointcut="execution(* com.savage.aop.MessageSender.*(..))", returning="retVal")  
  10.     public void afterReturning(JoinPoint joinPoint, Object retVal) {  
  11.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
  12.     }  
  13. }  

    这里和Before Advice有点不同的是,在定义Poincut表示式时,多了一个returning属性,用于指定目标方法执行完后的返回值。
       XML文件中的设置与LogBeforeAdvice的相似(将logBeforeAdvice的定义改为logAfterReturning的定义),不再列举。

    五、Around Advice:基于XML Sechma
    在Spring 2.0中,Around Advice不用实现org.aoplliance.intercept.MethodInterceptor接口,但Advice的方法必须返回对象,并且必须定义一个ProceedingJoinPoint参数,例如:
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4.   
  5. public class LogAroundAdvice {  
  6.     public void invoke(ProceedingJoinPoint joinPoint) {  
  7.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
  8.         Object retVal = joinPoint.proceed();  
  9.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
  10.         return retVal;  
  11.     }  
  12. }  

        XML中的设置如下:
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.     http://www.springframework.org/schema/aop  
  8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  10.       
  11.     <bean id="logAroundAdvice" class="com.savage.aop.LogAroundAdvice"></bean>  
  12.       
  13.     <aop:config>  
  14.         <aop:aspect id="logAround" ref="logAroundAdvice">  
  15.             <aop:around   
  16.                 pointcut="execution(* com.savage.aop.MessageSender.*(..))"   
  17.                 method="invoke"/>  
  18.         </aop:aspect>  
  19.     </aop:config>  
  20. </beans>  

    六、Around Advice:基于Annotation
    和Before Advice相似,使用@Around来表示Around Advice
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.Aspect;  
  5. import org.aspectj.lang.annotation.Around;  
  6.   
  7. @Aspect  
  8. public class AfterReturningAdvice {  
  9.     @Around("execution(* com.savage.aop.MessageSender.*(..))")  
  10.     public void invoke(ProceedingJoinPoint joinPoint) {  
  11.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
  12.         Object retVal = joinPoint.proceed();  
  13.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
  14.         return retVal;  
  15.     }  
  16. }  

XML文件中的设置与LogBeforeAdvice的相似(将logBeforeAdvice的定义改为logAroundAdvice的定义),不再列举。

    七、Throw Advice:基于XML Sechma
    在Spring 2.0中,Throw Advice不用实现org.springframework.aop.ThrowsAdvice接口,但Advice的方法必须定义Throwable(或其子类)参数,例如:
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4.   
  5. public class LogThrowingAdvice {  
  6.     public void afterThrowing (JoinPoint joinPoint, Throwable throwable) {  
  7.         System.out.println("Logging when throwing " + joinPoint.getSignature().getName());  
  8.     }  
  9. }  

    在XML的设置如下:
xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.     http://www.springframework.org/schema/aop  
  8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  10.       
  11.     <bean id="logThrowingAdvice" class="com.savage.aop.LogThrowingAdvice"></bean>  
  12.       
  13.     <aop:config>  
  14.         <aop:aspect id="logThrowing" ref="logThrowingAdvice">  
  15.             <aop:after-throwing   
  16.                 pointcut="execution(* com.savage.aop.MessageSender.*(..))"  
  17.                 throwing="throwable"  
  18.                 method="afterThrowing"/>  
  19.         </aop:aspect>  
  20.     </aop:config>  
  21. </beans>  

    在<aop:after-throwing></aop:after-throwing>中必须定义throwing属性,指定方法中的throwable参数。Spring将根据异常类型决定是否调用afterThrowing方法。

    八、Throw Advice:基于Annotation
java 代码
  1. package com.savage.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.annotation.Aspect;  
  5. import org.aspectj.lang.annotation.AfterThrowing;  
  6.   
  7. @Aspect  
  8. public class AfterThrowingAdvice {  
  9.     @AfterThrowing(pointcut="execution(* com.savage.aop.MessageSender.*(..))", throwing="throwable")  
  10.     public void afterThrowing(JoinPoint joinPoint, Throwable throwable) {  
  11.         System.out.println("Logging when throwing " + joinPoint.getSignature().getName());  
  12.     }  
分享到:
评论

相关推荐

    spring 2.0使用AOP实例(基于Annotation的配置方式)

    这篇博客文章将探讨如何在Spring 2.0中使用AOP实例,特别是通过注解来实现。 首先,我们需要了解AOP的基本概念。AOP的核心是切面(Aspect),它封装了横切关注点,即那些跨越多个对象的行为或责任。在Spring中,切...

    SPRING2.0中文文档

    Spring 2.0 是一个里程碑式的版本,它在Java企业级开发中扮演着核心角色,为开发者提供了丰富的功能和灵活性。这份全中文的Spring 2.0技术文档是学习和理解这一版本的重要参考资料,旨在帮助中国开发者更好地掌握...

    spring 2.0使用AOP实例(基于XML的配置方式)

    本实例将探讨Spring 2.0版本中如何利用AOP(面向切面编程)来实现横切关注点的解耦。AOP是Spring框架的一个重要特性,它允许我们编写与业务逻辑无关的代码,如日志、事务管理、性能监控等,并在适当的时候自动插入到...

    spring2.0中文手册及使用指南 chm

    Spring 2.0 是一个非常重要的Java框架,它在企业级应用开发中占据了核心地位,尤其是在基于Java的轻量级应用程序上下文(IoC)和面向切面编程(AOP)方面。本手册和使用指南提供了全面的Spring 2.0相关知识,包括其...

    Spring2.0中文教程

    Spring 2.0是Spring框架的一个重要版本,它在Java企业级应用开发中扮演着核心角色。本教程将深入探讨...文档`spring2.0-reference_final_zh_cn.chm`将详细阐述这些概念和技术,帮助你成为一名熟练的Spring开发者。

    spring2.0中文参考手册.rar

    1. **AOP(面向切面编程)增强**:Spring 2.0 提供了更强大的面向切面编程支持,允许开发者定义更复杂的切面,包括基于注解的切点表达式和更多的通知类型。这使得代码更加整洁,业务逻辑与系统服务(如事务管理)...

    spring2.0 中文教程

    在Spring 2.0版本中,引入了许多重要的改进和新特性,使得开发者能够更加高效地进行开发工作。以下是对Spring 2.0的一些关键知识点的详细解释: 1. **依赖注入 (Dependency Injection, DI)**:Spring 2.0继续强化了...

    Spring 2.0中文API(chm格式)

    在Spring 2.0中,容器引入了更多扩展点,如BeanPostProcessor和InstantiationAwareBeanPostProcessor,使得开发者可以在特定时刻介入bean的创建过程,实现自定义逻辑。 除此之外,Spring 2.0还强化了与其它开源框架...

    spring 2.0中文参考手册

    手册中还会详细讲解Spring的安全、测试、邮件服务等模块,帮助开发者全方位理解和掌握Spring 2.0框架。通过阅读《Spring 2.0中文参考手册》,开发者不仅可以学习到Spring的基本用法,还能了解到如何将Spring应用于...

    Spring2.0宝典源代码

    通过《Spring2.0宝典》的源代码,学习者可以逐步探索上述功能的实际运用,了解每个特性在项目中的实现方式,从而提升对Spring框架的理解和使用能力。在阅读源代码的过程中,建议结合书中的讲解,按照章节顺序逐步...

    spring2.0中文开发参考手册(CHM)

    《Spring 2.0中文开发参考手册》是针对Spring框架2.0版本的一份...通过阅读这份手册,开发者不仅可以深入学习Spring 2.0的技术细节,还能了解到如何将这些技术应用到实际项目中,实现高效、可扩展的Java企业级应用。

    spring2.0 jar包

    以下将详细阐述Spring 2.0中的关键知识点: 一、依赖注入(Dependency Injection,DI) 依赖注入是Spring的核心特性之一,它允许开发者通过配置文件或注解来管理对象间的依赖关系,而非硬编码在类内部。Spring 2.0...

    详尽的Spring2.0学习提纲

    Spring 2.0是Java开发中的一个里程碑,它在企业级应用开发中扮演着至关重要的角色,特别是对于依赖注入(IoC)和面向切面编程(AOP)的支持。本学习提纲旨在为初学者提供一份详尽的Spring 2.0学习指南,帮助他们系统...

    spring2.0中文开发参考手册(CHM)

    Spring框架是Java开发中的一个核心组件,尤其在企业级应用中广泛使用,它通过依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)等原则,简化了应用的构建和管理。...

    spring 2.0中文参考文档

    它广泛应用于Spring的配置和AOP中。 10. **Spring Test**: Spring提供了测试支持,包括单元测试和集成测试框架,使得测试Spring应用变得更加方便。 11. **消息支持**: Spring对JMS(Java Message Service)...

    spring2.0完整开发包

    Spring 2.0在AOP方面进行了增强,支持更多的通知类型,包括环绕通知,使切面的实现更为灵活。 Spring MVC是用于构建Web应用程序的模块,它提供了模型-视图-控制器架构,将业务逻辑、数据处理和用户界面分离。在...

    Spring2.0

    9. **AOP代理**:Spring 2.0支持JDK动态代理和CGLIB代理,允许用户选择最适合其需求的代理实现。 10. **更多扩展点**:Spring 2.0增加了许多新的API和扩展点,如事件监听、应用上下文、消息源等,为开发者提供了更...

Global site tag (gtag.js) - Google Analytics