1、注解形式
1.1先自定义注解
package com.wck.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
String wck() default "1";
}
1.2 aop 类
package com.wck.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Slf4j
@Aspect
@Component
public class AOPAnnotationAspect {
/**
* 注解切点
*/
@Pointcut(value = "@annotation(com.wck.annotation.MyAnnotation)")
public void annotationPointCut() {}
@Before(value = "annotationPointCut()")
public void doBefore(JoinPoint joinPoint){
System.out.println("注解的前置通知");
}
// @After(value = "annotationPointCut()")
// public void doAfter(ProceedingJoinPoint joinPoint){
//
// }
@Around(value = "annotationPointCut()")
public void doAround(ProceedingJoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
}
//
// @AfterReturning(value = "annotationPointCut()")
// public void doAfterReturning(JoinPoint joinPoint){
// MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// Method method = signature.getMethod();
//
//
// }
//
// @AfterThrowing(value = "annotationPointCut()")
// public void doAfterThrowing(ProceedingJoinPoint joinPoint){
//
// }
}
使用aop的方法上加上自定义的注解就可以
2、表达式
package com.wck.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Slf4j
@Aspect
@Component
public class AOPExecutionAspect {
/**
* 表达式切点
*/
@Pointcut("execution(* com.wck.controller..*.*(..))")
// @Pointcut("execution(* com.wck.controller.UserController.getUserList(..))")
private void executionPointCut() {}
@Before(value = "executionPointCut()")
public void doBefore(JoinPoint joinPoint){
System.out.println("表达式的前置通知");
}
// @After(value = "executionPointCut()")
// @Order(value = 3)
// public void doAfter(JoinPoint joinPoint){
// System.out.println("表达式的后置通知");
//
// }
@Around(value = "executionPointCut()")
public Object aroundMethodExecution(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// 在目标方法执行前后进行逻辑处理,并控制方法的执行
// 必须返回一个Object类型的结果,通常使用proceedingJoinPoint.proceed()来执行目标方法,并获取方法的返回值
// 可以在方法执行前后进行额外的逻辑处理
System.out.println("环绕通知");
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
try {
System.out.println("前置通知返回执行的方法:"+methodName+ Arrays.asList(proceedingJoinPoint.getArgs()));
result = proceedingJoinPoint.proceed();
System.out.println("返回通知的结果是:"+result);
}catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("异常返回");
}
System.out.println("后置通知:"+methodName);
result =100;
return result;
}
//
// @AfterReturning(value = "executionPointCut()", returning = "result")
// public void afterReturningMethodExecution(Object result) {
// // 在目标方法正常返回后执行的逻辑,可以获取方法的返回值
// System.out.println("表达式的前置通知");
// }
//
// @AfterThrowing(value = "executionPointCut()", throwing = "exception")
// public void afterThrowingMethodExecution(Exception exception) {
// // 在目标方法抛出异常后执行的逻辑,可以获取方法抛出的异常
// System.out.println("表达式的异常通知");
// }
}