zoukankan      html  css  js  c++  java
  • 详解Spring 框架中切入点 pointcut 表达式的常用写法

    Spring AOP 用户可能会经常使用 execution 切入点指示符。执行表达式的格式如下:

    1
    2
    3
    execution(modifiers-pattern?
    ret-type-pattern declaring-type-pattern?
    name-pattern(param-pattern)throws-pattern?)

    除了返回类型模式(上面代码片断中的 ret-type-pattern),名字模式和参数模式以外, 所有的部分都是可选的。返回类型模式决定了方法的返回类型必须依次匹配一个连接点。 你会使用的最频繁的返回类型模式是*,它代表了匹配任意的返回类型。 一个全限定的类型名将只会匹配返回给定类型的方法。名字模式匹配的是方法名。你可以使用*通配符作为所有或者部分命名模式。 参数模式稍微有点复杂,()匹配了一个不接受任何参数的方法, 而(..)则匹配了一个接受任意数量参数的方法(零或者更多)。模式(*)匹配了一个接受一个任何类型的参数的方法。 模式(*,String)匹配了一个接受两个参数的方法,第一个可以是任意类型, 第二个则必须是 String 类型。更多的信息请参阅 AspectJ 编程指南中 语言语义的部分。下面给出一些通用切入点表达式的例子。

    任意公共方法的执行:

    1
    execution(public * *(..))

    任何一个名字以 set 开始的方法的执行:

    1
    execution(* set*(..))

    AccountService 接口定义的任意方法的执行:

    1
    execution(* com.xyz.service.AccountService.*(..))

    在 service 包中定义的任意方法的执行:

    1
    execution(* com.xyz.service.*.*(..))

    在 service 包或其子包中定义的任意方法的执行:

    1
    execution(* com.xyz.service..*.*(..))

    在 service 包中的任意连接点(在 Spring AOP 中只是方法执行):

    1
    within(com.xyz.service.*)

    在 service 包或其子包中的任意连接点(在 Spring AOP 中只是方法执行):

    1
    within(com.xyz.service..*)

    实现 AccountService 接口的代理对象的任意连接点 (在 Spring AOP 中只是方法执行):

    1
    this(com.xyz.service.AccountService)

    实现 AccountService 接口的目标对象的任意连接点 (在 Spring AOP 中只是方法执行):

    1
    target(com.xyz.service.AccountService)

    任何一个只接受一个参数,并且运行时所传入的参数是 Serializable 接口的连接点(在 Spring AOP 中只是方法执行):

    1
    args(java.io.Serializable)

    请注意在例子中给出的切入点不同于execution(* *(Java.io.Serializable)),args 版本只有在动态运行时候传入参数是 Serializable 时才匹配,而 execution 版本在方法签名中声明只有一个 Serializable 类型的参数时候匹配。

    目标对象中有一个 @Transactional 注解的任意连接点 (在 Spring AOP 中只是方法执行):

    1
    @target(org.springframework.transaction.annotation.Transactional)

    任何一个目标对象声明的类型有一个 @Transactional 注解的连接点 (在 Spring AOP 中只是方法执行):

    1
    @within(org.springframework.transaction.annotation.Transactional)

    任何一个执行的方法有一个 @Transactional 注解的连接点 (在 Spring AOP 中只是方法执行):

    1
    @annotation(org.springframework.transaction.annotation.Transactional)

    任何一个只接受一个参数,并且运行时所传入的参数类型具有 @Classified 注解的连接点(在 Spring AOP 中只是方法执行):

    1
    @args(com.xyz.security.Classified)

    任何一个在名为 tradeService 的 Spring bean 之上的连接点 (在 Spring AOP 中只是方法执行):

    1
    bean(tradeService)

    任何一个在名字匹配通配符表达式*Service的 Spring bean 之上的连接点 (在 Spring AOP 中只是方法执行):

    1
    bean(*Service)

    其中,thistagartargs、 @target @with、 @annotation@args在绑定表单中更加常用。

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 相关阅读:
    Caliburn micro 学习笔记...
    First steps with Caliburn Micro in Windows Phone 8 系列文章
    WPF and Silverlight.ComboBox 如何通过 Binding IsDropDownOpen 实现下拉菜单展开
    http各个状态码的详解
    点阵字库产生的原理
    Windows 服务调试方法(基于.net framwork4.6)
    关于.net Core 笔记
    JS+ google.maps.api 实现基本的导航功能
    C# 遍历控件检查是否有被选中的项(通用)
    C#编程习惯
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/13941158.html
Copyright © 2011-2022 走看看