execution 表达式用法汇总


本文章向大家介绍切入点表达式execution(),主要包括切入点表达式execution()使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

用于描述方法 【掌握】

语法:execution(修饰符  返回值  包.类.方法名(参数) throws异常)

修饰符,一般省略

         public                公共方法

         *                        任意

返回值,不能省略

         void                   返回没有值

         String                返回值字符串

         *                       任意

包,[省略]

         com.gyf.crm                      固定包

         com.gyf.crm.*.service    crm包下面子包任意 (例如:com.gyf.crm.staff.service)

         com.gyf.crm..                   crm包下面的所有子包(含自己)

         com.gyf.crm.*.service..  crm包下面任意子包,固定目录service,service目录任意包

类,[省略]

         UserServiceImpl                       指定类

         *Impl                                          以Impl结尾

         User*                                          以User开头

         *                                                  任意

方法名,不能省略

         addUser                                     固定方法

         add*                                                    以add开头

         *Do                                             以Do结尾

         *                                                  任意

(参数)

         ()                                                  无参

         (int)                                             一个整型

         (int ,int)                                       两个

         (..)                                                参数任意

throws ,可省略,一般不写

案例1

execution(* com.gyf.crm.*.service..*.*(..))

案例2:

<aop:pointcut expression="execution(* com.gyf.crm.service.*.*(..)) ||  execution(* com.gyf.*Do.*(..))" id="myPointCut"/>

示例:

   
    <bean id="userService" class="com.gyf.service.UserServiceImpl">bean>
    <bean id="studentService" class="com.gyf.service.StudentService">bean>

    
    <bean id="myAspect" class="com.gyf.aspect.MyAspect">bean>


    
    <aop:config  proxy-target-class="true">
        
        <aop:pointcut id="myPointcut" expression="execution(* com.gyf.service.*.*(..))"/>

        
        <aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut">aop:advisor>
    aop:config>

1.切所有controller下的请求

项目结构

 任意公共方法的执行:

execution(public * (…))

public可以省略, 第一个 代表方法的任意返回值 第二个参数代表任意包+类+方法 (…)任意参数

配置切入点:

@Pointcut("execution(* com.gcol.qy.web.system.controller..*.*(..))")

public void conPoint(){}

表达式结构解释如下:

标识符

 含义
execution()  表达式的主体
第一个“ * ”符号  表达返回值的类型任意
com.gcol.qy.web.system.controller AOP所切的服务的包名,即,需要进行横切的业务员类
包名后面的 “ .. ”  表示当前包及子包
第二个“ * ”  表示类名,* 即所有类
.*(..)  表示任何方法名,括号表示参数,两个点表示任何参数类型

如果需要多个切入点可以用 || 符号 如下:

@Pointcut("execution(* com.gcol.qy.web.system.controller..*.*(..)) || execution(* com.gcol.qy.web.system.api..*.*(..))")

public void conPoint(){}

xml 配 ref=“dataSourceInterceptor” 执行的方法类



<aop:config>

 <aop:aspect id="dataSourceAspect2" ref="dataSourceInterceptor">

  <aop:pointcut id="daoThree" expression="execution(* com.gcol.qy.web.system.service.kpi.KpiServiceImpl.*(..))

  || execution(*  com.gcol.qy.web.system.service.team.TeamServiceImpl.getFamilyPremiun(..))

  || execution(*  com.gcol.qy.web.system.service.team.TeamServiceImpl.getSalesmanPremium(..))"/>

  <aop:before pointcut-ref="daoThree" method="setDataSourceKpi"/>

 aop:aspect>

aop:config>

Spring中execution语法

语法格式

execution(修饰符 返回类型 切入点类 切入点方法(参数) 异常抛出)

  修饰符: 可选,支持通配符,(public/private/protected)
  返回类型: 必填,支持通配符,可以使用 * 来匹配所有的返回值类型
  切入点类: 可选,支持通配符,指定切入点类
  切入点方法: 必填,支持通配符,指定要匹配的方法名,可以使用"*"通配符来匹配所有方法
  参数: 若无可不填,指定方法声明中的形参列表,支持两个通配符,即*和…


其中*代表一个任意类型的参数,而…代表零个或多个任意类型的参数

  () 匹配一个不接受任何参数的方法
  (…) 匹配一个接受任意数量参数的方法,可以是零个或多个
  (*) 匹配一个接受一个任何类型的参数的方法,只能是一个
  (*,String) 匹配一个接受两个参数的方法,其中第一个参数是任意类型,第二个参数必须是String类型
  异常抛出: 可选,支持通配符,指定方法声明抛出的异常

常用实例



<aop:pointcut expression="execution(public * *(..))" id="pt"/> 



<aop:pointcut expression="execution(* save*(..))" id="pt"/>



<aop:pointcut expression="execution(* com.shore.dao.impl.UserDao.save(..))" id="pt"/>



<aop:pointcut expression="execution(* com.shore.dao.impl.UserDao.*(..))" id="pt"/>



<aop:pointcut expression="execution(* com..*.*(..))" id="pt"/>





<aop:pointcut expression="execution(* com.shore.dao.impl.UserDao.save(..)) || execution(* com.shore.dao.impl.MessageDao.save(..))" id="pt" />

<aop:pointcut expression="execution(* com.shore.dao.impl.UserDao.save(..)) or execution(* com.shore.dao.impl.MessageDao.save(..))" id="pt" />

  

<aop:pointcut expression="execution(* com.shore.dao.impl.UserDao.save(..)) && execution(* com.shore.dao.impl.MessageDao.save(..))" id="pt" />

<aop:pointcut expression="execution(* com.shore.dao.impl.UserDao.save(..)) and execution(* com.shore.dao.impl.MessageDao.save(..))" id="pt" />



<aop:pointcut expression="!execution(* com.shore.dao.impl.UserDao.save(..))" id="pt"/> 

<aop:pointcut expression=" not execution(* com.shore.dao.impl.UserDao.save(..))" id="pt"/>