`
- 浏览:
131447 次
- 性别:
- 来自:
深圳
-
AOP实现(三)——Spring 2.0中Pointcut的定义
在Spring 2.0中,Pointcut的定义包括两个部分:Pointcut表示式(expression)和Pointcut签名(signature)。让我们先看看execution表示式的格式:
java 代码
- execution(modifier-pattern?
- ret-type-pattern
- declaring-type-pattern?
- name-pattern(param-pattern)
- 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 代码
- @Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
-
- private void log(){}
然后要使用所定义的Pointcut时,可以指定Pointcut签名,如
java 代码
上面的定义等同与:
java 代码
- @Before("execution(* com.savage.aop.MessageSender.*(..))")
Pointcut定义时,还可以使用&&、||、!运算,如:
java 代码
- @Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
- private void logSender(){}
-
- @Pointcut("execution(* com.savage.aop.MessageReceiver.*(..))")
- private void logReceiver(){}
-
- @Pointcut("logSender() || logReceiver()")
- private void logMessage(){}
这个例子中,logMessage()将匹配任何MessageSender和MessageReceiver中的任何方法。
还可以将一些公用的Pointcut放到一个类中,以供整个应用程序使用,如:
java 代码
- package com.savage.aop;
-
- import org.aspectj.lang.annotation.*;
-
- public class Pointcuts {
- @Pointcut("execution(* *Message(..))")
- public void logMessage(){}
-
- @Pointcut("execution(* *Attachment(..))")
- public void logAttachment(){}
-
- @Pointcut("execution(* *Service.*(..))")
- public void auth(){}
- }
在使用这些Pointcut时,指定完整的类名加上Pointcut签名就可以了,如:
java 代码
- package com.savage.aop;
-
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.*;
-
- @Aspect
- public class LogBeforeAdvice {
- @Before("com.sagage.aop.Pointcuts.logMessage()")
- public void before(JoinPoint joinPoint) {
- System.out.println("Logging before " + joinPoint.getSignature().getName());
- }
- }
当基于XML Sechma实现Advice时,如果Pointcut需要被重用,可以使用<aop:pointcut></aop:pointcut>来声明Pointcut,然后在需要使用这个Pointcut的地方,用pointcut-ref引用就行了,如:
xml 代码
- <aop:config>
- <aop:pointcut id="log"
- expression="execution(* com.savage.simplespring.bean.MessageSender.*(..))"/>
- <aop:aspect id="logging" ref="logBeforeAdvice">
- <aop:before pointcut-ref="log" method="before"/>
- <aop:after-returning pointcut-ref="log" method="afterReturning"/>
- </aop:aspect>
- </aop:config>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
在Spring中,切面可以由一个类定义,这个类包含了通知(Advice)——也就是实际执行的代码,以及切点(Pointcut)——定义了何时执行这些通知。 接下来,我们将深入到基于注解的配置。在Spring 2.0中,我们可以使用...
假设我们需要在一个简单的服务层中实现事务管理功能,可以定义一个切面来自动处理事务: ```xml <aop:config> <aop:aspect id="txAspect" ref="transactionManager"> <aop:pointcut id="businessService" ...
Spring框架中的AOP实现主要依赖于Spring的核心特性之一——IoC(Inversion of Control,控制反转)。Spring AOP构建在IoC容器之上,使得开发者可以利用Spring框架本身提供的强大功能来实现AOP。Spring 2.0版本对AOP...
首先,`spring-aspects.jar`是Spring框架的AOP模块,它包含了Spring对AspectJ库的支持,使得开发者可以在Spring环境中无缝地实现AOP。AspectJ是一种全面的AOP解决方案,能够处理编译时和运行时的切面,而Spring AOP...
在给定的描述中,提到了与Struts2.0一起使用的其他技术,如Spring 2.0和iBatis,这形成了一种常见的Java企业级架构——SSI(Spring+Struts2+iBatis)。Spring作为一个强大的依赖注入(DI)和面向切面编程(AOP)框架...
然而,Spring 3.1进一步深化了这一理念,通过其强大的依赖注入(DI)机制,以及对OSGi 4.2的支持,使得开发者能够在更广泛的环境中实现组件的动态加载与更新,从而实现了更高层次的模块化和可插拔性。 #### 二、...
2. **AOP(面向切面编程)**:Spring 3.0强化了对AOP的支持,允许开发者定义切面,将关注点如日志、事务管理等与业务逻辑分离。切面通过通知(Advice)、切点(Pointcut)、切面(AopProxy)等概念实现。 3. **Bean管理**:...