一.切入点表达式-execution


访问修饰符(public/private等),包名.类名.,throws 异常都可以省略,但是建议包名.类名.不要省略,否则的话匹配范围太大影响程序执行效率。
*主要用来匹配单个参数,通配任意返回值、包名、类名、方法名、任意类型的一个参数,也可以通配包、类、方法名的一部分。
..匹配任意层级的包,或者任意类型、任意个数的参数。


二.演示
下面我们在代码中进行演示
package com.gjw.aop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;/*** 目标:探究AOP中的切入点表达式-execution* execution主要根据方法的返回值、包名、类名、方法名、方法参数等信息来匹配,语法为:* execution(访问修饰符? 返回值 包名.类名.?方法名(方法参数) throws 异常?)* 可以使用通配符描述切入点* *:单个独立的任意符号,可以通配任意返回值、包名、类名、方法名、任意类型的一个参数,也可以通配包、类、方法名的一部分* execution(* com.*.service.*.update*(*))* ..:多个连续的任意符号,可以通配任意层级的包,或者任意类型、任意个数的参数* execution(* com.itheima..DeptService.*(..))* 注意事项:* 根据业务需要,可以使用 且(&&)、或(||)、非(!)来组合比较复杂的切入点表达式*///@Aspect
@Slf4j
@Component
public class MyAspect6 {
// @Pointcut("execution(public void com.gjw.service.impl.DeptServiceImpl.deleteById(java.lang.Integer))") // 通配DeptServiceImpl中的deleteById方法
// @Pointcut("execution(void com.gjw.service.impl.DeptServiceImpl.deleteById(java.lang.Integer))")
// @Pointcut("execution(void deleteById(java.lang.Integer))") // 会与DeptServiceImpl中的deleteById方法和DeptMapper中的deleteById方法都匹配上,因此会造成通知运行两次 包名,类名不建议省略// @Pointcut("execution(void com.gjw.service.DeptService.deleteById(java.lang.Integer))") // 根据接口名来匹配
// @Pointcut("execution(void com.gjw.service.DeptService.*(java.lang.Integer))") // 匹配DeptService接口下返回值为void的所有方法
// @Pointcut("execution(* com.*.service.DeptService.*(*))") // 匹配返回值为任意类型的com下的任意包下的service包下的DeptService类下的为单个形参任意方法
// @Pointcut("execution(* com.gjw.service.*Service.delete*(*))") // 匹配返回值为任意类型的com.gjw.service包下的以Service结尾的类下的以delete开头的传递参数为单个形参的方法// @Pointcut("execution(* com.gjw.service.DeptService.*(..))") // 匹配DeptService接口下的所有方法,不管形参个数类型是多少
// @Pointcut("execution(* com..DeptService.*(..))") 匹配com包下所有叫做DeptService的类中的所有方法
// @Pointcut("execution(* com..*.*(..))") // 匹配com包下的所有方法 .. 慎用@Pointcut("execution(* com.gjw.service.DeptService.deleteById(java.lang.Integer)) ||" +"execution(* com.gjw.service.DeptService.list())") // 匹配DeptServiceImpl类中的list方法和deleteById方法private void pt(){}@Before("pt()")public void before() {log.info("MyAspect6......before......");}
}
第一个定义访问修饰符为public,返回值为void,匹配com.gjw.service.impl包下的DeptServiceImpl类中的deleteById方法,方法的参数类型为Interger类型,个数为一个。
第二个省略方法的访问修饰符,返回值为void,匹配com.gjw.service.impl包下的DeptServiceImpl类中的deleteById方法,方法的参数类型为Interger类型,个数为一个。
第三个省略方法的访问修饰符,返回值为void,省略包名和类名。只匹配deleteById方法,方法的参数类型为Interger类型,个数为一个。这样的话会匹配到两个,一个是DeptServiceImpl.java类下的,另一个是DeptMapper接口下的,这样通知方法会执行两次。
第四个省略方法的访问修饰符,返回值为void,匹配接口下的deleteById方法,方法的参数类型为Interger类型,个数为一个。
第五个省略方法的访问修饰符,返回值为void,匹配接口下的任意方法,方法的参数类型为Interger类型,个数为一个。
第六个省略方法的访问修饰符,返回值为任意类型,匹配com包下的任意一级包下的service包下的DeptService类中的所有方法,方法的参数类型为任意类型,个数为一个。
第七个省略方法的访问修饰符,返回值为任意类型,匹配com.gjw.service包下的以Service结尾的类或接口中的以delete开头的方法,方法的参数类型为任意类型,个数为一个。
第八个省略方法的访问修饰符,返回值为任意类型,匹配com.gjw.service包下的DeptService接口中的所有方法,方法的参数类型为任意类型,个数为任意个数。
第九个省略方法的访问修饰符,返回值为任意类型,匹配com包下任意级包下的DeptService接口中的所有方法,方法的参数类型为任意类型,个数为任意个数。
第十个省略方法的访问修饰符,返回值为任意类型,匹配com包下的所有方法,方法的参数类型为任意类型,个数为任意个数。
第十一个匹配DeptServiceImpl类中的list方法和deleteById方法,我们使用或进行连接。