//@Pointcut("execution(public void com.itxiaoli.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
//表示使用全称的方式进行书写表达式,不进行任何的匹配,这个也是精确的写法
//@Pointcut("execution(void com.itxiaoli.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
//@Pointcut("execution(void delete(java.lang.Integer))")
//包名.类名不建议省略 public可以省略不写,如果是私有的记得写上
//包名类名如果省略的话,不同包下面的方法如果名称也是delete也会运行这个通知
//@Pointcut("execution(void com.itxiaoli.service.DeptService.delete(java.lang.Integer))")
//@Pointcut("execution(void com.itxiaoli.service.DeptService.*(java.lang.Integer))")
//使用*号可以进行任意一个字符的省略,例如:DeptService.*代表DeptService下面任意的方法
//注意*号仅仅表示一个参数
//@Pointcut("execution(* com.*.service.DeptService.*(*))")
//@Pointcut("execution(* com.itxiaoli.service.*Service.delete*(*))")
//@Pointcut("execution(* com.itxiaoli.service.DeptService.*(..))")
//使用..进行省略通配的话,是通配任意字符,不限制数量
//@Pointcut("execution(* com..DeptService.*(..))")
//@Pointcut("execution(* com..*.*(..))")
//@Pointcut("execution(* *(..))") //慎用,这样写也不是不行,但是很模糊后期不好维护
@Pointcut("execution(* com.itxiaoli.service.DeptService.list()) || " +
"execution(* com.itxiaoli.service.DeptService.deleteDeptById(..))")
//也可以使用||符号,精确的书写多个确定的方法
private void pt(){}
@Before("pt()")
public void before(){
log.info("MyAspect ... before ...");
}
使用execution来匹配多个无规则的具体的方法比较困难
使用annotation注解就比较简单,@Pointcut("@annotation(com.example.aop.MyLog)")
这一串代码的意思是添加自定义注解MyLog的路径
//切面类
@Slf4j
@Aspect
@Component
public class MyAspect {
//匹配DeptServiceImpl中的 list() 和 delete(Integer id)方法
//@Pointcut("execution(* com.itheima.service.DeptService.list()) || execution(* com.itheima.service.DeptService.delete(java.lang.Integer))")
@Pointcut("@annotation(com.example.aop.MyLog)")
private void pt(){}
@Before("pt()")
public void before(){
log.info("MyAspect ... before ...");
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyLog {
}
在新建类的时候选择注解即可(@)
添加上以后就可以看自己想要那个方法,显示在控制台,或者运行信息保存到日志文件。
会更灵活