5-3 配置切入點Pointcut

在AOP中通知Advice和一個切入點表達式關聯
execution用於匹配方法執行的連接點

pointcut

execution: AspectJ與Spring AOP 都支持:

  • execution(public **(…)) 切入點爲執行所有public方法時
  • execution(* set*(..)) 切入點爲執行所有set開始的方法時
  • execution(* com.xyz.service.AccountService.*(..)) 切入點爲執行AccountService類中的所有方法時
  • execution(* com.xyz.service..(..)) 切入點爲執行com.xyz.service包下的所有方法時
  • execution(* com.xyz.service…(. .)) 切入點爲執行com.xyz.service包及其子包下的所有方法時

以下只有Spring AOP才支持

  • within(com.xyz.service.*) (only in Spring AOP)
  • within(com.xyz.service..*) (only in Spring AOP)
    within 用於匹配指定類型內的方法執行
  • this(com.xyz.service.AccountService) (only in Spring AOP)
    this 用於匹配當前AOP代理對象類型的執行方法
  • targer(com.xyz.service.AccountService) (only in Spring AOP)
    targer 用於匹配當前目標對象類型的執行方法
  • args(java.io.Serializable) (only in Spring AOP)
    @target(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
    args 用於匹配當前執行的方法傳入的參數爲指定類型的執行方法
  • @within(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
  • @annotation(org.springframework.transaction.annotation.Transactional) (only in Spring AOP)
  • @args(com.xyz.security.Classified) (only in Spring AOP)
  • bean(tradeService) (only in Spring AOP)
  • bean(*Service) (only in Spring AOP)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="moocAspect" class="com.torey.aop.schema.advice.MoocAspect"></bean>
    <bean id="aspectBiz" class="com.torey.aop.schema.advice.biz.AspectBiz"></bean>
    <aop:config>
        <aop:aspect id="moocAspectAOP" ref="moocAspect">
            <!--匹配AspectBiz類下的所有方法-->
            <aop:pointcut id="aspec" expression="execution(com.torey.aop.schema.advice.biz.AspectBiz.*(..))"/>
            <!--匹配以類名以Biz結尾的類的所有方法-->
           <!-- <aop:pointcut id="aspec" expression="execution(com.torey.aop.schema.advice.biz.*Biz.*(..))"/>-->
        </aop:aspect>
    </aop:config>
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章