Spring切入点表达式常用写法

Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的.
Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合.
args()
@args()
execution()
this()
target()
@target()
within()
@within()
@annotation
其中execution 是用的最多的,其格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必须的.
ret-type-pattern:可以为*表示任何返回值,全路径的类名等.
name-pattern:指定方法名,*代表所以,set*,代表以set开头的所有方法.
parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.
举例说明:
任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))

pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了Intf接口的所有类,如果Intf不是接口,限定Intf单个类.
this(com.test.spring.aop.pointcutexp.Intf)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.

带有@Transactional标注的所有类的任意方法.
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
带有@Transactional标注的任意方法.
@annotation(org.springframework.transaction.annotation.Transactional)
***> @within和@target针对类的注解,@annotation是针对方法的注解

参数带有@Transactional标注的方法.
@args(org.springframework.transaction.annotation.Transactional)
参数为String类型(运行是决定)的方法.
args(String)

Pointcut 可以通过Java注解和XML两种方式配置,如下所示:

    <aop:config>  
        <aop:aspectrefaop:aspectref="aspectDef">  
            <aop:pointcutidaop:pointcutid="pointcut1"expression="execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))"/>  
            <aop:before pointcut-ref="pointcut1" method="beforeAdvice" />  
        </aop:aspect>  
    </aop:config>  
      
    @Component  
    @Aspect  
    public class AspectDef {  
        //@Pointcut("execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")  
        //@Pointcut("within(com.test.spring.aop.pointcutexp..*)")  
        //@Pointcut("this(com.test.spring.aop.pointcutexp.Intf)")  
        //@Pointcut("target(com.test.spring.aop.pointcutexp.Intf)")  
        //@Pointcut("@within(org.springframework.transaction.annotation.Transactional)")  
        //@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")  
        @Pointcut("args(String)")  
        public void pointcut1() {  
        }  
        @Before(value = "pointcut1()")  
        public void beforeAdvice() {  
            System.out.println("pointcut1 @Before...");  
        }  


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

对Service包上所有方法的切面配置
        <aop:config>
                <aop:pointcut id="serviceOperation"
                                            expression="execution(* *..service*..*(..))"/>
                <aop:advisor pointcut-ref="serviceOperation"
                                         advice-ref="txAdvice"/>
        </aop:config>
 
表达式所处位置如上pointcut的位置。
 
配置这个为了更好控制切面上的事务,下面是一个事物配置的简单例子:
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
                <tx:attributes>
                        <tx:method name="delete*" rollback-for="Exception"/>
                        <tx:method name="save*" rollback-for="Exception"/>
                        <tx:method name="update*" rollback-for="Exception"/>
                        <tx:method name="*" read-only="true" rollback-for="Exception"/>
                </tx:attributes>
        </tx:advice>
 
通过切面、通知的配置,就为所有的delete/save/update开头的方法添加上了一致性事务,对其他方法添加上了只读事务。
这个还不够细,如果要写更为详细的控制,就需要研究AspectJ切点配置的语法了,其实研究这些标准,还不如拿几个例子看看,解决实际问题就行了。就像写正则表达式一样,标准明摆着,要写好却不容易,从例子着手就能快速上手和领悟。
以下文档来自Spring中文开发指南2.5文档,由满江红开源组织翻译:
Spring AOP 用户可能会经常使用 execution切入点指示符。执行表达式的格式如下:

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

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

    任意公共方法的执行:

    execution(public * *(..))

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

    execution(* set*(..))

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

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

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

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

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

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

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

    within(com.xyz.service.*)

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

    within(com.xyz.service..*)

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

    this(com.xyz.service.AccountService)

    'this'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得代理对象在通知体内可用。
    实现AccountService接口的目标对象的任意连接点 (在Spring AOP中只是方法执行):

    target(com.xyz.service.AccountService)

    'target'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得目标对象在通知体内可用。
    任何一个只接受一个参数,并且运行时所传入的参数是Serializable 接口的连接点(在Spring AOP中只是方法执行)

    args(java.io.Serializable)

    'args'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得方法参数在通知体内可用。
    请注意在例子中给出的切入点不同于 execution(* *(java.io.Serializable)): args版本只有在动态运行时候传入参数是Serializable时才匹配,而execution版本在方法签名中声明只有一个 Serializable类型的参数时候匹配。
    目标对象中有一个 @Transactional 注解的任意连接点 (在Spring AOP中只是方法执行)

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

    '@target'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
    任何一个目标对象声明的类型有一个 @Transactional 注解的连接点 (在Spring AOP中只是方法执行):

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

    '@within'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
    任何一个执行的方法有一个 @Transactional 注解的连接点 (在Spring AOP中只是方法执行)

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

    '@annotation'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
    任何一个只接受一个参数,并且运行时所传入的参数类型具有@Classified 注解的连接点(在Spring AOP中只是方法执行)

    @args(com.xyz.security.Classified)

    '@args'在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
    任何一个在名为'tradeService'的Spring bean之上的连接点 (在Spring AOP中只是方法执行):

    bean(tradeService)

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

    bean(*Service)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值