`

【转】AOP实现(三)——Spring 2.0中Pointcut的定义

阅读更多

【转】AOP实现(三)——Spring 2.0中Pointcut的定义

关键字: Pointcut的定义
Spring 2.0 中, Pointcut 的定义包括两个部分: Pointcut 表示式 (expression) Pointcut 签名 (signature) 。让我们先看看 execution 表示式的格式:
java 代码
  1. execution(modifier-pattern?
  2.           ret-type-pattern
  3.           declaring-type-pattern?
  4.           name-pattern(param-pattern)
  5.           throws-pattern?)

括号中各个 pattern 分别表示修饰符匹配( modifier-pattern? )、返回值匹配( ret-type-pattern )、类路径匹配( declaring-type-pattern? )、方法名匹配( name-pattern )、参数匹配( (param-pattern) )、异常类型匹配( throws-pattern? ),其中后面跟着“ ? ”的是可选项。
在各个 pattern 中可以使用“ * ”来表示匹配所有。在 (param-pattern) 中,可以指定具体的参数类型,多个参数间用“ , ”隔开,各个也可以用“ * ”来表示匹配任意类型的参数,如 (String) 表示匹配一个 String 参数的方法; (*,String) 表示匹配有两个参数的方法,第一个参数可以是任意类型,而第二个参数是 String 类型;可以用 (..) 表示零个或多个任意参数。
现在来看看几个例子:
1 execution(* *(..))
表示匹配所有方法
2 execution(public * com. savage.service.UserService.*(..))
表示匹配 com.savage.server.UserService 中所有的公有方法
3 execution(* com.savage.server..*.*(..))
表示匹配 com.savage.server 包及其子包下的所有方法
除了 execution 表示式外,还有 within this target args Pointcut 表示式。 一个 Pointcut 定义由 Pointcut 表示式和 Pointcut 签名组成,例如:
java 代码
  1. //Pointcut表示式
  2. @Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
  3. //Point签名
  4. privatevoid log(){}                             

然后要使用所定义的 Pointcut 时,可以指定 Pointcut 签名,如
java 代码
  1. @Before("og()")

上面的定义等同与:
java 代码
  1. @Before ( "execution(* com.savage.aop.MessageSender.*(..))" )

Pointcut 定义时,还可以使用 && || ! 运算,如:
java 代码
  1. @Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
  2. privatevoid logSender(){}

  3. @Pointcut("execution(* com.savage.aop.MessageReceiver.*(..))")
  4. privatevoid logReceiver(){}

  5. @Pointcut("logSender() || logReceiver()")
  6. privatevoid logMessage(){}

这个例子中, logMessage() 将匹配任何 MessageSender MessageReceiver 中的任何方法。
还可以将一些公用的 Pointcut 放到一个类中,以供整个应用程序使用,如:
java 代码
  1. package com.savage.aop;

  2. import org.aspectj.lang.annotation.*;

  3. publicclass Pointcuts {
  4.    @Pointcut("execution(* *Message(..))")
  5.    publicvoid logMessage(){}

  6.    @Pointcut("execution(* *Attachment(..))")
  7.    publicvoid logAttachment(){}

  8.    @Pointcut("execution(* *Service.*(..))")
  9.    publicvoid auth(){}
  10. }

在使用这些 Pointcut 时,指定完整的类名加上 Pointcut 签名就可以了,如:
java 代码
  1. package com.savage.aop;

  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.*;

  4. @Aspect
  5. publicclass LogBeforeAdvice {
  6.    @Before("com.sagage.aop.Pointcuts.logMessage()")
  7.    publicvoid before(JoinPoint joinPoint) {
  8.       System.out.println("Logging before " + joinPoint.getSignature().getName());
  9.    }
  10. }

当基于 XML Sechma 实现 Advice 时,如果 Pointcut 需要被重用,可以使用 <aop:pointcut> </aop:pointcut> 来声明 Pointcut ,然后在需要使用这个 Pointcut 的地方,用 pointcut-ref 引用就行了,如:
xml 代码
 
  1. <aop:config>  
  2.     <aop:pointcut id="log"   
  3.           expression="execution(* com.savage.simplespring.bean.MessageSender.*(..))"/>  
  4.     <aop:aspect id="logging" ref="logBeforeAdvice">  
  5.         <aop:before pointcut-ref="log" method="before"/>  
  6.         <aop:after-returning pointcut-ref="log" method="afterReturning"/>  
  7.     </aop:aspect>  
  8. </aop:config>  

分享到:
评论

相关推荐

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

    在Spring中,切面可以由一个类定义,这个类包含了通知(Advice)——也就是实际执行的代码,以及切点(Pointcut)——定义了何时执行这些通知。 接下来,我们将深入到基于注解的配置。在Spring 2.0中,我们可以使用...

    spring 学习第二天

    假设我们需要在一个简单的服务层中实现事务管理功能,可以定义一个切面来自动处理事务: ```xml &lt;aop:config&gt; &lt;aop:aspect id="txAspect" ref="transactionManager"&gt; &lt;aop:pointcut id="businessService" ...

    精通 Spring_2.x_6-9 精通 Spring_2.x_6-9

    Spring框架中的AOP实现主要依赖于Spring的核心特性之一——IoC(Inversion of Control,控制反转)。Spring AOP构建在IoC容器之上,使得开发者可以利用Spring框架本身提供的强大功能来实现AOP。Spring 2.0版本对AOP...

    spring-aspects.jar

    首先,`spring-aspects.jar`是Spring框架的AOP模块,它包含了Spring对AspectJ库的支持,使得开发者可以在Spring环境中无缝地实现AOP。AspectJ是一种全面的AOP解决方案,能够处理编译时和运行时的切面,而Spring AOP...

    struts2.0总结

    在给定的描述中,提到了与Struts2.0一起使用的其他技术,如Spring 2.0和iBatis,这形成了一种常见的Java企业级架构——SSI(Spring+Struts2+iBatis)。Spring作为一个强大的依赖注入(DI)和面向切面编程(AOP)框架...

    spring-3.1.0中文版api帮助文档.pdf

    然而,Spring 3.1进一步深化了这一理念,通过其强大的依赖注入(DI)机制,以及对OSGi 4.2的支持,使得开发者能够在更广泛的环境中实现组件的动态加载与更新,从而实现了更高层次的模块化和可插拔性。 #### 二、...

    Spring3.0源码3

    2. **AOP(面向切面编程)**:Spring 3.0强化了对AOP的支持,允许开发者定义切面,将关注点如日志、事务管理等与业务逻辑分离。切面通过通知(Advice)、切点(Pointcut)、切面(AopProxy)等概念实现。 3. **Bean管理**:...

Global site tag (gtag.js) - Google Analytics