同じエントリポイントの場合は、存在する場合、複数の通知方法がカットされます、通常、我々はより多くの注釈をマークする必要があり、各注釈エントリポイントは、現在の通知方法を実行したときに、春を告げる、表現を使用する必要があります。エントリポイントを変更する必要がある場合、それはより変更する必要があります。この不都合を回避するために、再利用のポイントカット表現を促進し、これ@Pointcutを得、エントリポイントをマークするために使用することができます。
例:
以前のセクションクラスなどStudentServiceLogger
のため、StudentServiceImpl
1日、私は場合にのみ、1カット方式を変更したい場合、あなたはこれらの4つの音の表現のためのエントリポイントを変更する必要があるクラスのすべてのメソッドは、4つの注目に切断されます。
@Aspect
@Component
public class StudentServiceLogger {
@Before("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
public void doBefore(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法执行前...");
System.out.println("参数为:"+ Arrays.asList(args));
}
@After("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
public void doAfter(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法执行后...");
}
@AfterReturning(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )" , returning = "returning")
public void doReturn(JoinPoint joinPoint,Object returning){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法返回,返回值为:"+returning);
}
@AfterThrowing(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )",throwing = "ex")
public void doThrow(JoinPoint joinPoint,Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法异常,异常信息为:"+ex.getMessage());
}
}
@Pointcut例では使用し
、このクラスの次のセクションに完全に同等の上記の例を、。
@Aspect
@Component
public class StudentServiceLogger {
@Pointcut("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
public void basePointCut(){}
@Before("basePointCut()")
public void doBefore(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法执行前...");
System.out.println("参数为:"+ Arrays.asList(args));
}
@After("basePointCut()")
public void doAfter(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法执行后...");
}
@AfterReturning(value = "basePointCut()" , returning = "returning")
public void doReturn(JoinPoint joinPoint,Object returning){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法返回,返回值为:"+returning);
}
@AfterThrowing(value = "basePointCut()",throwing = "ex")
public void doThrow(JoinPoint joinPoint,Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法异常,异常信息为:"+ex.getMessage());
}
}
私たちは宣言basePointCut()
空のメソッドを、戻り値は、上でタグ付けするための方法で無効にする必要があり@Pointcut
、我々は4つの通知方法の公共の表現、そのエントリポイントを入力する前に、コメント。カット式の前の場所に続いて、後続のすべての使用、交換basePointCut()
同じエントリポイント表現を指し示すためには、メソッド名を。それ以降のすべての変更は、私は変更することができますbasePointCut()
方法にカット式を。