java aop两种形式注解和表达式

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("表达式的异常通知");
//    }

    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值