package geektime.spring.springbucks.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @Author kevin
* @Description 执行顺序
* @DATE Created on 2019-03-20 10:18
*/
@Aspect
@Component
@Slf4j
public class KevinAspect {
private long startTime;
private long endTime;
/**
* 声明一个切点
*/
//@Pointcut("execution(* geektime.spring.springbucks.repository..*(..))")
@Pointcut("execution(* geektime.spring.springbucks.service..*(..))")
private void kevin(){
}
@Before("kevin()")
public void Before(JoinPoint joinPoint){
String classname = joinPoint.getTarget().getClass().getSimpleName();
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("@before Execute! --class name: " + classname + ", method name: " + methodName + " " + args );
}
@Around("kevin()")
public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("@Around:执行目标方法之前...");
Object obj = proceedingJoinPoint.proceed();
System.out.println("@Around:执行目标方法之后...");
System.out.println("@Around:被织入的目标对象为:" + proceedingJoinPoint.getTarget());
System.out.println("@Around:原返回值:" + obj + ",这是返回结果的后缀");
return obj;
}
@AfterThrowing("kevin()")
public void AfterThrowing(){
System.out.println("异常通知....");
}
@After("kevin()")
public void After(JoinPoint point){
System.out.println("@After:模拟释放资源...");
System.out.println("@After:目标方法为:" +
point.getSignature().getDeclaringTypeName() +
"." + point.getSignature().getName());
System.out.println("@After:参数为:" + Arrays.toString(point.getArgs()));
System.out.println("@After:被织入的目标对象为:" + point.getTarget());
}
@AfterReturning("kevin()")
public void AfterReturning(JoinPoint point){
System.out.println("@AfterReturning:模拟日志记录功能...");
System.out.println("@AfterReturning:目标方法为:" +
point.getSignature().getDeclaringTypeName() +
"." + point.getSignature().getName());
System.out.println("@AfterReturning:参数为:" +
Arrays.toString(point.getArgs()));
System.out.println("@AfterReturning:返回值为:" );
System.out.println("@AfterReturning:被织入的目标对象为:" + point.getTarget());
}
}
对应的service
public void test(){
log.info("方法执行test- > 测试返回false" );
//return false;
}
执行结果
@Around:执行目标方法之前…
@before Execute! --class name: CoffeeService, method name: test []
2019-03-20 14:42:18.649 INFO 9836 — [ main] g.s.springbucks.service.CoffeeService : 方法执行test- > 测试返回false
@Around:执行目标方法之后…
@Around:被织入的目标对象为:geektime.spring.springbucks.service.CoffeeService@526f6427
@Around:原返回值:null,这是返回结果的后缀
@After:模拟释放资源…
@After:目标方法为:geektime.spring.springbucks.service.CoffeeService.test
@After:参数为:[]
@After:被织入的目标对象为:geektime.spring.springbucks.service.CoffeeService@526f6427
@AfterReturning:模拟日志记录功能…
@AfterReturning:目标方法为:geektime.spring.springbucks.service.CoffeeService.test
@AfterReturning:参数为:[]
@AfterReturning:返回值为:
@AfterReturning:被织入的目标对象为:geektime.spring.springbucks.service.CoffeeService@526f6427