抽取前:
package com.spring.spring5.annotaion;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增强的类
@Aspect//表示生成代理对象
@Component
public class UserProxy {
//前置通知
@Before(value = "execution(* com.spring.spring5.annotaion.User.add(..))")//表示作为前置通知
public void before(){
System.out.println("Before......");
}
//最终通知,不管有没有异常在方法之后都执行
@After(value = "execution(* com.spring.spring5.annotaion.User.add(..))")
public void after(){
System.out.println("After......");
}
//后置通知(返回通知),方法无异常结束时执行
@AfterReturning(value = "execution(* com.spring.spring5.annotaion.User.add(..))")
public void afterReturning(){
System.out.println("AfterReturning......");
}
//异常通知,add()有异常时才会执行
@AfterThrowing(value = "execution(* com.spring.spring5.annotaion.User.add(..))")
public void afterThrowing(){
System.out.println("AfterThrowing......");
}
//环绕通知,在方法之前和之后执行
@Around("execution(* com.spring.spring5.annotaion.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Around之前......");
proceedingJoinPoint.proceed();
System.out.println("Around之后......");
}
}
抽取后:
package com.spring.spring5.annotaion;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增强的类
@Aspect//表示生成代理对象
@Component
public class UserProxy {
//相同切入点抽取
@Pointcut("execution(* com.spring.spring5.annotaion.User.add(..))")
public void pointdemo() {
}
//前置通知
@Before(value = "pointdemo()")//表示作为前置通知
public void before(){
System.out.println("Before......");
}
//最终通知,不管有没有异常在方法之后都执行
@After(value = "pointdemo()")
public void after(){
System.out.println("After......");
}
//后置通知(返回通知),方法无异常结束时执行
@AfterReturning(value = "pointdemo()")
public void afterReturning(){
System.out.println("AfterReturning......");
}
//异常通知,add()有异常时才会执行
@AfterThrowing(value = "pointdemo()")
public void afterThrowing(){
System.out.println("AfterThrowing......");
}
//环绕通知,在方法之前和之后执行
@Around("pointdemo()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Around之前......");
proceedingJoinPoint.proceed();
System.out.println("Around之后......");
}
}
两者效果一致