`
反求诸己
  • 浏览: 534791 次
  • 性别: Icon_minigender_1
  • 来自: 湖南娄底
社区版块
存档分类
最新评论

spring 中Pointcut的定义(转)

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

java 代码
  1. //Pointcut表示式
  2. @Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
  3. //Point签名
  4. private void 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. private void logSender(){}
  3. @Pointcut("execution(* com.savage.aop.MessageReceiver.*(..))")
  4. private void logReceiver(){}
  5. @Pointcut("logSender() || logReceiver()")
  6. private void logMessage(){}

这个例子中,logMessage()将匹配任何MessageSenderMessageReceiver中的任何方法。
还可以将一些公用的Pointcut
放到一个类中,以供整个应用程序使用,如:
java 代码
  1. package com.savage.aop;
  2. import org.aspectj.lang.annotation.*;
  3. public class Pointcuts {
  4.    @Pointcut("execution(* *Message(..))")
  5.    public void logMessage(){}
  6.    @Pointcut("execution(* *Attachment(..))")
  7.    public void logAttachment(){}
  8.    @Pointcut("execution(* *Service.*(..))")
  9.    public void 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. public class LogBeforeAdvice {
  6.    @Before("com.sagage.aop.Pointcuts.logMessage()")
  7.    public void 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 Boot的@Pointcut注解

    总的来说,@Pointcut注解是Spring Boot AOP中非常有用的一部分,它允许我们轻松地定义切点并应用通知来实现面向切面编程。通过这种方式,我们可以有效地在应用程序中实现横切关注点的功能,提高代码的可维护性和重用...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring AOP中文教程

    Spring framework是很有前途的AOP技术。...pointcut定义了需要注入advice的位置,通常是某个特定的类的一个public方法。advisor是pointcut和advice的装配器,是将advice注入主程序中预定义位置的代码。

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring AOP配置源码

    @Component("userService")等价于在spring配置文件中定义一个&lt;bean id="userService"/&gt; @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....

    开源框架 Spring Gossip

    Before Advice After Advice Around Advice Throw Advice Pointcut、Advisor Pointcut 定义了 Advice 的应用时机,在 Spring 中,使用 PointcutAdvisor 将 Pointcut 与 Advice 结合成为一个...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring Security 中文教程.pdf

    5.5. Spring Security中的访问控制(验证) 5.5.1. 安全和AOP建议 5.5.2. 安全对象和AbstractSecurityInterceptor 5.5.2.1. 配置属性是什么? 5.5.2.2. RunAsManager 5.5.2.3. AfterInvocationManager ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    spring aop 示例

    spring aop的编程例子,里面包括基本的PointCut定义,通知定义,引入。可以直接运行,方便对照书籍学习。

    spring security 参考手册中文版

    12.2.3使用RequestPostProcessor在Spring MVC测试中以用户身份运行 106 作为用户在Spring MVC测试中使用注释运行 108 12.2.4测试HTTP基本认证 109 12.3 SecurityMockMvcRequestBuilders 109 12.3.1测试基于表单的...

    spring学习之六“AOP使用spring静态配置文件的实现”

    NULL 博文链接:https://whp0731.iteye.com/blog/357015

    Spring Security-3.0.1中文官方文档(翻译版)

    这次发布的Spring Security-3.0.1 是一个bug fix 版,主要是对3.0 中存在的一些问题进 行修 正。文档中没有添加新功能的介绍,但是将之前拼写错误的一些类名进行了修正,建议开发 者以这一版本的文档为参考。 ...

    Spring框架生态流程框架图-执行运行路程图

    执行Spring框架的运行路程通常包括以下几个重要的步骤: 应用程序启动:应用程序启动时,Spring框架会加载并...在AOP中,开发人员可以定义切点(Pointcut)和增强(Advice),以在应用程序的不同位置插入额外的逻辑。

    Spring.html

    PlatFormTransactionManager:平台事务管理器:定义了commit/rollback Mybatis/jdbc:DataSourceTransactionManager Hibernater:HibernaterTransactionManager TransactionManagerDifinition 传播行为:A--&gt;B,...

    Spring AOP详细介绍.docx

    一 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,...Spring中的AOP代理可以使JDK动态代理,也可以是CGLIB代理,前者基于接口,后者基于子类

    Java八股文-Spring AOP

    在Spring中,像 BeforeAdvice等还带有方位信息 通知是直译过来的结果,我个人感觉叫做“业务增强”更合适 对照代码就是拦截器定义的 相关方法,通知分为如下几种: 1. 前置通知(before):在执行业务代码前做些操作,...

    Spring AOP源码深度解析:掌握Java高级编程核心技术

    Spring AOP(面向切面编程)是Java高级编程中的重要组成部分,它允许程序员以声明的方式处理关注点(例如日志、事务管理等),而不是通过硬编码。本文深入分析了Spring AOP的实现机制,让读者能够更好地理解和应用这...

    springboot学习思维笔记.xmind

    web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware ...

Global site tag (gtag.js) - Google Analytics