`
- 浏览:
468039 次
- 性别:
- 来自:
深圳
-
【转】AOP实现(三)——Spring 2.0中Pointcut的定义
关键字: 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.*(..))")
- privatevoid log(){}
然后要使用所定义的
Pointcut
时,可以指定
Pointcut
签名,如
java 代码
上面的定义等同与:
java 代码
-
@Before
(
"execution(* com.savage.aop.MessageSender.*(..))"
)
Pointcut
定义时,还可以使用
&&
、
||
、
!
运算,如:
java 代码
- @Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
- privatevoid logSender(){}
- @Pointcut("execution(* com.savage.aop.MessageReceiver.*(..))")
- privatevoid logReceiver(){}
- @Pointcut("logSender() || logReceiver()")
- privatevoid logMessage(){}
这个例子中,
logMessage()
将匹配任何
MessageSender
和
MessageReceiver
中的任何方法。
还可以将一些公用的
Pointcut
放到一个类中,以供整个应用程序使用,如:
java 代码
- package com.savage.aop;
- import org.aspectj.lang.annotation.*;
- publicclass Pointcuts {
- @Pointcut("execution(* *Message(..))")
- publicvoid logMessage(){}
- @Pointcut("execution(* *Attachment(..))")
- publicvoid logAttachment(){}
- @Pointcut("execution(* *Service.*(..))")
- publicvoid auth(){}
- }
在使用这些
Pointcut
时,指定完整的类名加上
Pointcut
签名就可以了,如:
java 代码
- package com.savage.aop;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.*;
- @Aspect
- publicclass LogBeforeAdvice {
- @Before("com.sagage.aop.Pointcuts.logMessage()")
- publicvoid 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管理**:...