Spring aop execution() 是最常见的切点函数

execution() 是最常见的切点函数,语法形式为

excution(<修饰符模式> ? <返回类型模式> <方法名模式>(<参数模式>) <异常模式>?) 
实例代码
 1 package com.aop.learn.aspectj;
 2 
 3 import org.aspectj.lang.annotation.Aspect;
 4 import org.aspectj.lang.annotation.Before;
 5 import org.springframework.stereotype.Component;
 6 
 7 /**
 8  * @author songshuiyang
 9  * @title: execution() 实例
10  * @description:
11  * @date 2017/11/18
12  */
13 @Aspect
14 @Component
15 public class ExecutionAspect {
16 
17     /**
18      * 匹配所有目标类的public方法
19      */
20     @Before("execution(public * *(..))")
21     public void beforeAspect(){
22 
23     }
24     /**
25      * 匹配所有以To为后缀的方法
26      */
27     @Before("execution(* *To(..))")
28     public void beforeAspect1(){
29 
30     }
31     /**
32      * 匹配Waiter接口中的所有方法
33      */
34     @Before("execution(* com.aop.learn.service.Writer.*(..))")
35     public void beforeAspect2(){
36 
37     }
38     /**
39      * 匹配Waiter接口中及其实现类的方法
40      */
41     @Before("execution(* com.aop.learn.service.Writer+.*(..))")
42     public void beforeAspect3(){
43 
44     }
45     /**
46      * 匹配 com.aop.learn.service 包下所有类的所有方法
47      */
48     @Before("execution(* com.aop.learn.service.*(..))")
49     public void beforeAspect4(){
50 
51     }
52     /**
53      * 匹配 com.aop.learn.service 包,子孙包下所有类的所有方法
54      */
55     @Before("execution(* com.aop.learn.service..*(..))")
56     public void beforeAspect5(){
57 
58     }
59     /**
60      * 匹配 包名前缀为com的任何包下类名后缀为ive的方法,方法必须以Smart为前缀
61      */
62     @Before("execution(* com..*.*ive.Smart*(..))")
63     public void beforeAspect6(){
64 
65     }
66     /**
67      * 匹配 save(String name,int age) 函数
68      */
69     @Before("execution(* save(String,int))")
70     public void beforeAspect7(){
71 
72     }
73     /**
74      * 匹配 save(String name,*) 函数 第二个参数为任意类型
75      */
76     @Before("execution(* save(String,*))")
77     public void beforeAspect8(){
78 
79     }
80     /**
81      * 匹配 save(String name,..) 函数 除第一个参数固定外,接受后面有任意个入参且入参类型不限
82      */
83     @Before("execution(* save(String,..))")
84     public void beforeAspect9(){
85 
86     }
87     /**
88      * 匹配 save(String+) 函数  String+ 表示入参类型是String的子类
89      */
90     @Before("execution(* save(String+))")
91     public void beforeAspect10(){
92 
93     }
94 }

 

posted on 2017-11-18 17:57  伸手摘太阳  阅读(4472)  评论(0编辑  收藏  举报

导航