在 spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
的基础上,
这里说的是,切点表达式 的 提取 和 复用
目录

MyAdviceCopy类
java
package com.english.advice;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* 切点表达式 的 提取 和 复用
* 方式1、当前类中提取(不推荐)
* 首先:定义一个空方法 public void pc(){}
* 然后,@Pointcut("execution(* com..impl.*.*(..))") 修饰 pc 方法
* 最后,在别的 增强注解中使用即可,例如:
* @After("pc()")
* public void after(){
* System.out.println("after");
* }
*
* 方式2、创建一个存储切点的类
* 只不过使用的时候,要这样:类的权限定符.方法名()
*
*/
@Aspect
@Component
public class MyAdviceCopy {
@Pointcut("execution(* com..impl.*.*(..))")
public void pc(){}
// 方式1 的使用
@After("pc()")
public void after(){
System.out.println("after");
}
// 方式2 的使用
@Before("com.english.pointcut.MyPointCut.pc()")
public void start(){
System.out.println("start");
}
// 方式2 的使用
@AfterThrowing("com.english.pointcut.MyPointCut.mypc()")
public void error(){
System.out.println("error");
}
}
MyPointCut 类(上面用到了)
java
package com.english.pointcut;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
public class MyPointCut {
@Pointcut("execution(* com..impl.*.*(..))")
public void pc(){}
@Pointcut("execution(* com.english.service.impl.*.*(..))")
public void mypc(){}
}